Fetching a lazy OneToOne entity fetches every other OneToOne entities within the same object

456 Views Asked by At

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 hibernate 5.4.1-Final.

I use bytecode enhancement as below :

<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;

}

So when I do

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 =?

Even if I fetch the B in an HQLQuery, I still have a query to 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=?

I've tried to do a double mapping (OneToOne with mappedBy specified) without success. The PK of B and C are the same as A.

I expect the a.getB(); not to trigger the fetch of C. Is this a hibernate bug ? I can't find anything regarding this behaviour in their documentation.

Is my mapping correct ?

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that it works as designed :) b and c belong to the same default "LazyGroup". If any of b or c needs to be loaded, the whole group will.

Quote from the bytecode enhancer documentation :

Lazy attributes can be designated to be loaded together and this is called a "lazy group". By default, all singular attributes are part of a single group, meaning that when one lazy singular attribute is accessed all lazy singular attributes are loaded. Lazy plural attributes, by default, are each a lazy group by themselves. This behavior is explicitly controllable through the @org.hibernate.annotations.LazyGroup annotation.

Just add the @LazyGroup("b") on the b field, and @LazyGroup("c") on c and it should work as expected : b will be loaded only on getB(), anc c on getC().

More on this here and here.