| Hello, I think I have a major issue with Lazy Loading in OneToOne relations. Using
Entity entity = hibernateTemplate.get(Entity.class, id);
when I hit a entity.getChild() which is a OneToOne relation, every other OneToOne relations are loaded as well I use the hibernate-enhance-maven-plugin as follow :
<build>
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<configuration>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>false</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Example with A, B, C, D. A is a table with only a column ID as a PK. B, C and D share the same ID as A (so their PK is an FK towards A.ID)
@Entity
@Table(name = "A")
public class A {
@Id
@Column(name = "ID")
private int id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID")
@LazyToOne(LazyToOneOption.NO_PROXY)
private B b;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID")
@LazyToOne(LazyToOneOption.NO_PROXY)
private C c;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID")
@LazyToOne(LazyToOneOption.NO_PROXY)
private D d;
public B getB() {
return b;
}
public C getC() {
return c;
}
public D getD() {
return d;
}
}
@Entity
@Table(name = "B")
public class B {
@Id
@Column(name = "ID")
private int id;
}
same goes for C and D (no need to paste the classes). In my DAO with an entityManager :
A a = entityManager.find(A.class, 1);
a.getB();
I forked the github test-case and created a testcase https://github.com/vmeunier/hibernate-test-case-templates please see ORMUnitTestCase.java. As I call the getB(), you can see all the others properties are fetched. Any help is appreciated. If I have anything wrong with my mapping please point it out for me. I have tried to make the relations BiDirectionnal without success, the use of @MapsId as well... Nothing works. We used bytecode enhancement in Hibernate 3 and we weren't facing this problem. Thanks in advance. |