3

I have the following problem (pseudo-java-code):

Let me a class A,B,C with the following relationships:

@Entity
@Table(name = "A")
public class A {

  @OneToMany(mappedBy = "a")
  private B b; 

}


@Entity
@Table(name = "B")
public class B {

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "a_id")
  private A a;

  @OneToOne(mappedBy = "b", fetch = FetchType.LAZY)
  private C c;

}


@Entity
@Table(name = "C")
public class C {

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "b_id")
  private B b;

}

I'm using JpaRepository with @Query annotation and I implemented the following query:

@Query("SELECT DISTINCT(a) FROM A a "
        + "LEFT JOIN FETCH a.b as b"
        + "WHERE a.id = :id ")
A findById(@Param("id") Integer id);

I want retrieve the informations about class A and B, but not C. Somehow (I don't know why) the query try to retrive also the relation between B and C. And then, with hibernate, start the lazy invocation for retrieving C.

Naturally, if I fetch also the relation between B and C (adding LEFT JOIN FETCH b.c as c) that's not happen.

My question is, why? Why I'm forced to fetch all nested relations and not only the ones which I need?

thank you. Carmelo

carmelolg
  • 484
  • 5
  • 16
  • It's the default behavior to load lazily all the properties that is annotated with `@OneToMany`. If you don't want then don't use that annotation. – Erfan Ahmed Dec 05 '17 at 11:03
  • @ErfanAhmedEmon I don't think that the problem was the `@OneToMany` relation, but is explained here: https://stackoverflow.com/a/47652652/4149078 Thank you for your comment. – carmelolg Dec 05 '17 at 13:10

2 Answers2

2

Nullable @OneToOne relation are always eager fetched as explained in this post Making a OneToOne-relation lazy

Unconstrained (nullable) one-to-one association is the only one that can not be proxied without bytecode instrumentation. The reason for this is that owner entity MUST know whether association property should contain a proxy object or NULL and it can't determine that by looking at its base table's columns due to one-to-one normally being mapped via shared PK, so it has to be eagerly fetched anyway making proxy pointless.

Zeromus
  • 4,014
  • 8
  • 27
  • 36
  • Yes, I found the same post, and I understood why. Is definitly the best explication about this behavior. Thank you very much. – carmelolg Dec 05 '17 at 13:07
0

Shot in the dark, but there are some issues with lazy-loading @OneToOne-relationships. At least in older versions of Hibernate. I think (but can't seem to find any documentation) that this was fixed in one of the newer versions, so if you are not using a new version of Hibernate, try upgrading.

Tobb
  • 10,619
  • 4
  • 48
  • 67