We're updating the issue view to help you get more done. 

All @OneToOne entities within the same object are loaded while accessing just one of them

Description

Hello, I think I have a major issue with Lazy Loading in OneToOne relations.

Using

1 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 :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <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)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 @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; } }
1 2 3 4 5 6 7 8 @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 :

1 2 3 4 5 6 7 8 A a = entityManager.find(A.class, 1); // Triggers : Hibernate: select a0_.id as id1_0_0_ from a a0_ where a0_.id=? a.getB(); // Triggers : // Hibernate: select d0_.id as id1_3_0_ from d d0_ where d0_.id=? // Hibernate: select c0_.id as id1_2_0_ from c c0_ where c0_.id=? // Hibernate: select b0_.id as id1_1_0_ from b b0_ where b0_.id=?

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.

Environment

Java 8, Spring Boot 2.1.6.RELEASE, Hibernate 5.3.10.Final

Status

Assignee

Unassigned

Reporter

Vincent Meunier

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Feedback Requested

2019/07/15

Components

Affects versions

5.4.0

Priority

Critical