使用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 的获取。这是一个休眠错误吗?我在他们的文档中找不到有关此行为的任何信息。
我的映射正确吗?
largeQ
相关分类