使用Entity entity = hibernateTemplate.get(Entity.class, id);when I hit a entity.getChild()which is a OneToOne relation,所有其他 OneToOne 关系也被加载。我使用休眠 5.4.1-Final。我使用字节码增强如下:<configuration> <failOnError>true</failOnError> <enableLazyInitialization>true</enableLazyInitialization> <enableDirtyTracking>false</enableDirtyTracking> <enableAssociationManagement>true</enableAssociationManagement></configuration>A.java@Entity@Table(name = "A")public class A { @Id @Column(name = "ID_A") private String id; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ID_A") @LazyToOne(LazyToOneOption.NO_PROXY) private B b; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ID_A") @LazyToOne(LazyToOneOption.NO_PROXY) private C c;...getters/setters...B.java@Entity@Table(name = "B")public class B { @Id @Column(name = "ID_A") private String id;}C.java@Entity@Table(name = "C")public class C { @Id @Column(name = "ID_A") private String id;}所以当我做A a = hibernateTemplate.get(A.class, "100"); // triggers an Hibernate query only on A entity. The B and C aren't fetched => OK// Hibernate: select a0_.ID_A as ID_A_27_0_ from A a0_ where a0_.ID_A=?a.getB(); // ERROR : triggers two queries : one on C and one on B// Hibernate: select c0_.ID_A as ID_A _26_0_ from C c0_ where c0_.ID_A =?// Hibernate: select b0_.ID_A as ID_A _13_0_ from B b0_ where b0_.ID_A =?即使我获取了B一个HQLQuery,我仍然有一个查询C:Query<A> queryA = hibernateTemplate.createHQLQuery("from A a join fetch a.b where a.id=:id", A.class);queryA.setParameter("id", "100");A a = queryA.uniqueResult(); // triggers an inner join// Hibernate: select a0_.as ID_A1_27_0_, b1_.ID_A as ID_A1_13_1_ from A a0_ inner join B b1_ on a0_.ID_A=b1_.ID_A where a0_.ID_A=? a.getB(); // KO -> triggers a query to select C !// Hibernate: select c0_.ID_A as ID_A1_26_0_ from C c0_ where c0_.ID_A=?我尝试进行双映射(指定了 mappedBy 的 OneToOne)但没有成功。B和C的PK和A一样。我希望a.getB();不会触发 C 的获取。这是一个休眠错误吗?我在他们的文档中找不到有关此行为的任何信息。我的映射正确吗?
1 回答
largeQ
TA贡献2039条经验 获得超7个赞
它似乎按设计工作:) b 和 c 属于相同的默认“LazyGroup”。如果需要加载 b 或 c 中的任何一个,则整个组都会加载。
引用字节码增强器文档:
惰性属性可以指定一起加载,这称为“惰性组”。默认情况下,所有单数属性都是单个组的一部分,这意味着当访问一个惰性单数属性时,将加载所有惰性单数属性。惰性复数属性,默认情况下,每个属性都是一个惰性组。此行为可通过 @org.hibernate.annotations.LazyGroup 注释显式控制。
@LazyGroup("b")
只需在 b 字段和 c 上添加,它应该按预期工作:b 将仅在, anc c on 上@LazyGroup("c")
加载。getB()
getC()
添加回答
举报
0/150
提交
取消