看了几个网页后,我仍然不明白为什么我的@ElementCollection会急切地加载。我的理解是,默认情况下任何@ElementCollection都应该总是懒洋洋地加载。正如您所看到的,情况并非如此--您将在本文末尾看到控制台的输出。我试图使用@ElementCollection(fetch=fetchType.lazy)注释强制执行惰性加载,但这并没有改变任何事情。有没有人可以解释一下,也可以说明一下,为了懒洋洋地加载,如何更改注释?
我有一个JavaFX应用程序。因此,我必须使用Properties和AccessType.Property。改变这是不可能的。此外,我使用的是:Java 11、Hibernate 5.4.15.Final、MySQL8、Spring boot 2.2.7
@Entity
@DiscriminatorValue(Term.PRODUCT)
@Access(AccessType.PROPERTY)
public class Product extends Term {
...
protected ListProperty<Double> marketSize;
protected ListProperty<Double> marketGrowth;
protected ListProperty<Integer> marketUnits;
...
public Product() {
...
marketSize = new SimpleListProperty<>();
marketGrowth = new SimpleListProperty<>();
marketUnits = new SimpleListProperty<>();
...
}
@ElementCollection(fetch = FetchType.LAZY)
public List<Double> getMarketSize() {
return marketSize;
}
public void setMarketSize(List<Double> marketSize) {
this.marketSize = new SimpleListProperty<Double>(FXCollections.observableArrayList(marketSize));
}
public ListProperty<Double> marketSizeProperty() {
return marketSize;
}
// Likewise for marketGrowth and marketUnits
...
}
@Service
public class TermService {
...
public ObservableList<Product> getAllProducts() {
ObservableList<Product> products = FXCollections.observableArrayList();
products.addAll(productRepository.findAll());
return products;
}
}
@Repository
public interface ProductRepository extends TermRepository<Product> {
}
@NoRepositoryBean`
public interface TermRepository<T extends Term> extends JpaRepository<T, Long> {
List<T> findByIdentifier(String indentifier);
List<T> findByGoal(String goal);
List<Product> findByParent(Term parent);
List<T> findAll();
T getById(Long id);
}
当我的业务逻辑从TermService调用getAllProducts()时,它会得到以下结果(提取)
DEBUG 09:01 o.h.e.j.s.SqlStatementLogger.logStatement:128: select term0_.id as id2_66_1_, term0_.goal as goal3_66_1_, term0_.identifier as identifi4_66_1_, term0_.level as level5_66_1_, term0_.parent_id as parent_46_66_1_, term0_.plannedBegin as plannedb6_66_1_, term0_.plannedEnd as plannede7_66_1_, term0_.state as state8_66_1_, term0_.fk_successor as fk_succ47_66_1_, term0_.fk_testplan as fk_test48_66_1_, term0_.timeUnit as timeunit9_66_1_, term0_.type as type10_66_1_, term0_.version as version11_66_1_, term0_.affection as affecti12_66_1_, term0_.classification as classif13_66_1_, term0_.classificationJustification as classif14_66_1_, term0_.customerCharacteristics as custome15_66_1_, term0_.customerDenomination as custome16_66_1_, term0_.devMaterialCosts as devmate17_66_1_, term0_.directive as directi18_66_1_, term0_.factoryCosts as factory19_66_1_, term0_.fixedCosts as fixedco20_66_1_, term0_.generalAdminCosts as general21_66_1_, term0_.iec60601 as iec22_66_1_, term0_.iec62304 as iec23_66_1_, term0_.iec62366 as iec24_66_1_, term0_.intendedUse as intende25_66_1_, term0_.iso13485 as iso26_66_1_, term0_.iso14155 as iso27_66_1_, term0_.iso14971 as iso28_66_1_, term0_.leasingCosts as leasing29_66_1_, term0_.manufacturingCosts as manufac30_66_1_, term0_.marketGeography as marketg31_66_1_, term0_.marketValueSegment as marketv32_66_1_, term0_.negativeImpact as negativ33_66_1_, term0_.prodMaterialCosts as prodmat34_66_1_, term0_.productLine_id as product49_66_1_, term0_.fk_prodowner as fk_prod50_66_1_, term0_.fk_prjmanager as fk_prjm51_66_1_, term0_.projectedSalesPrice as project35_66_1_, term0_.purpose as purpose36_66_1_, term0_.reach as reach37_66_1_, term0_.fk_regmanager as fk_regm52_66_1_, term0_.restriction as restric38_66_1_, term0_.rohs as rohs39_66_1_, term0_.salaryCosts as salaryc40_66_1_, term0_.salesAdminCosts as salesad41_66_1_, term0_.systemDescription as systemd42_66_1_, term0_.fk_sysengineer as fk_syse53_66_1_, term0_.technologyReadinessLevel as technol43_66_1_, term0_.unlike as unlike44_66_1_, term0_.varCosts as varcost45_66_1_, term0_.term_level as term_lev1_66_1_, term1_.id as id2_66_0_, term1_.goal as goal3_66_0_, term1_.identifier as identifi4_66_0_, term1_.level as level5_66_0_, term1_.parent_id as parent_46_66_0_, term1_.plannedBegin as plannedb6_66_0_, term1_.plannedEnd as plannede7_66_0_, term1_.state as state8_66_0_, term1_.fk_successor as fk_succ47_66_0_, term1_.fk_testplan as fk_test48_66_0_, term1_.timeUnit as timeunit9_66_0_, term1_.type as type10_66_0_, term1_.version as version11_66_0_, term1_.affection as affecti12_66_0_, term1_.classification as classif13_66_0_, term1_.classificationJustification as classif14_66_0_, term1_.customerCharacteristics as custome15_66_0_, term1_.customerDenomination as custome16_66_0_, term1_.devMaterialCosts as devmate17_66_0_, term1_.directive as directi18_66_0_, term1_.factoryCosts as factory19_66_0_, term1_.fixedCosts as fixedco20_66_0_, term1_.generalAdminCosts as general21_66_0_, term1_.iec60601 as iec22_66_0_, term1_.iec62304 as iec23_66_0_, term1_.iec62366 as iec24_66_0_, term1_.intendedUse as intende25_66_0_, term1_.iso13485 as iso26_66_0_, term1_.iso14155 as iso27_66_0_, term1_.iso14971 as iso28_66_0_, term1_.leasingCosts as leasing29_66_0_, term1_.manufacturingCosts as manufac30_66_0_, term1_.marketGeography as marketg31_66_0_, term1_.marketValueSegment as marketv32_66_0_, term1_.negativeImpact as negativ33_66_0_, term1_.prodMaterialCosts as prodmat34_66_0_, term1_.productLine_id as product49_66_0_, term1_.fk_prodowner as fk_prod50_66_0_, term1_.fk_prjmanager as fk_prjm51_66_0_, term1_.projectedSalesPrice as project35_66_0_, term1_.purpose as purpose36_66_0_, term1_.reach as reach37_66_0_, term1_.fk_regmanager as fk_regm52_66_0_, term1_.restriction as restric38_66_0_, term1_.rohs as rohs39_66_0_, term1_.salaryCosts as salaryc40_66_0_, term1_.salesAdminCosts as salesad41_66_0_, term1_.systemDescription as systemd42_66_0_, term1_.fk_sysengineer as fk_syse53_66_0_, term1_.technologyReadinessLevel as technol43_66_0_, term1_.unlike as unlike44_66_0_, term1_.varCosts as varcost45_66_0_, term1_.term_level as term_lev1_66_0_ from Term term0_ left outer join Term term1_ on term0_.id=term1_.fk_successor where term0_.fk_successor=?
DEBUG 09:01 o.h.e.j.s.SqlStatementLogger.logStatement:128: select marketgrow0_.Product_id as product_1_29_0_, marketgrow0_.marketGrowth as marketgr2_29_0_ from Product_marketGrowth marketgrow0_ where marketgrow0_.Product_id=?
DEBUG 09:01 o.h.e.j.s.SqlStatementLogger.logStatement:128: select marketsize0_.Product_id as product_1_31_0_, marketsize0_.marketSize as marketsi2_31_0_ from Product_marketSize marketsize0_ where marketsize0_.Product_id=?
DEBUG 09:01 o.h.e.j.s.SqlStatementLogger.logStatement:128: select marketunit0_.Product_id as product_1_33_0_, marketunit0_.marketUnits as marketun2_33_0_ from Product_marketUnits marketunit0_ where marketunit0_.Product_id=?
我只希望看到第一行,但不希望看到最后三行。
如果元素集合被注释(或xml映射)为LAZY,则除非有EntityGraph提示,否则不应加载该元素集合。尝试生成优化SQL的另一个JPA提供程序,如cmobilecom-jpa。
问题内容: 我在JPA实体中的延迟加载属性有问题。我读过许多类似的问题,但它们与spring或hibernate有关,并且他们的后代不适用或没有帮助。 该应用程序是在Wildfly应用程序服务器上运行的JEE和JPA2.1。有两个实体,DAO会话bean和servlet将它们放在一起: 当我运行此代码时,它失败并显示: 我对WebLogic / JPA1使用了非常相似的模式,并且运行平稳。任何的想
问题内容: 我以这样一种方式进行了JPA设置:如果我不使用延迟加载,则几乎将加载整个数据库。我也直接在模型上使用序列化,因此有时我需要初始化代理。 我只想在集合上使用延迟加载。急切地获取一些奇异实体的事实就很好了。但是,无论我如何尝试设置集合,我都永远不会得到代理集合,而我总是会得到完全加载的集合。 这是一些示例代码: 所以这里的问题是,当我检查调试器时,答复的persistantBag-list
我需要从Rest控件获取一个实体及其集合,但我对spring data JPA有一个非常奇怪的行为。 我对带有懒惰加载的实体有一种莫名其妙的关系。
问题内容: 我以这样一种方式进行了JPA设置:如果我不使用延迟加载,则几乎将加载整个数据库。我也直接在模型上使用序列化,因此有时我需要初始化代理。 我只想在集合上使用延迟加载。急切地获取某些奇异实体的事实就很好了。但是,无论我如何尝试设置集合,我都永远不会得到代理集合,而我总是会得到完全加载的集合。 这是一些示例代码: 所以这里的问题是,当我检查调试器时,答复的persistantBag-list
我正在使用Play Framework v.2.6.3和用于SQL访问的JPA解决方案。 当我在实体上设置FetchType.Lazy时,我会收到以下错误消息: play.api.http。HttpErrorHandlerExceptions$$anon$1:执行异常[[RuntimeException:java.lang.IllegalArgumentException:未能延迟初始化角色集合:
描述 (Description) 延迟加载可应用于图像,背景图像和淡入效果,如下所述 - 对于图像 要在图像上使用延迟加载,请按照给定的步骤进行操作 - 使用data-src属性而不是src属性来指定图像源。 将类lazy添加到图像。 <div class = "page-content"> ... <img data-src = "image_path.jpg" class = "l