How nested FETCH JOIN works on Hibernate using JpaRepository and @Query annotation?

carmelolg :

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

Zeromus :

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

JPA / Hibernateの春@Transactional対FETCH JOINを

分類Dev

Spring Data JpaRepository "JOIN FETCH"が重複を返す

分類Dev

How to efficiently update using JpaRepository

分類Dev

How to tell hibernate validator to validate only one annotation?

分類Dev

Reducing boilerplate code in Hibernate repositories/using Spring's JpaRepository interface in a desktop client-server application

分類Dev

How @scheduled annotation works in Spring Boot

分類Dev

SQLAlchemy: Using delete/update with a join query

分類Dev

How to perform join query in Firebase?

分類Dev

Understanding how pandas join works

分類Dev

How does pathVar Attribute of @MatrixVariable annotation works in Spring?

分類Dev

How acquiring shared and exclusive locks works in Hibernate

分類Dev

instead of fetching multiple tables using pyspark how can we execute join query using jdbc

分類Dev

JpaRepository native query not detecting parameters

分類Dev

How to join using a nested column in Spark dataframe

分類Dev

How to fetch all the batches for a query at one go using stored proc in azure SQL db?

分類Dev

insert using sub-query and inner join

分類Dev

Nested JSON fetch using jQuery

分類Dev

SQL Query Update using a join

分類Dev

Hibernate criteria using left join to filter results

分類Dev

Create a double join query using Rails Admin

分類Dev

how to change this sql query to hibernate query?

分類Dev

Laravel 4.2 Eloquent using lists() with a join query

分類Dev

How to fetch results from two tables using single query

分類Dev

JPA or Hibernate or Spring annotation to specify a query for a relationship?

分類Dev

Android: how to do an inner-join query in sqlite using the ContextResolver?

分類Dev

Hibernate Join query to fetch the data in Java?

分類Dev

Laravel join query using Laravel eloquent

分類Dev

php how to fetch multiple records and display in a query

分類Dev

How to exclude columns in hibernate join

Related 関連記事

  1. 1

    JPA / Hibernateの春@Transactional対FETCH JOINを

  2. 2

    Spring Data JpaRepository "JOIN FETCH"が重複を返す

  3. 3

    How to efficiently update using JpaRepository

  4. 4

    How to tell hibernate validator to validate only one annotation?

  5. 5

    Reducing boilerplate code in Hibernate repositories/using Spring's JpaRepository interface in a desktop client-server application

  6. 6

    How @scheduled annotation works in Spring Boot

  7. 7

    SQLAlchemy: Using delete/update with a join query

  8. 8

    How to perform join query in Firebase?

  9. 9

    Understanding how pandas join works

  10. 10

    How does pathVar Attribute of @MatrixVariable annotation works in Spring?

  11. 11

    How acquiring shared and exclusive locks works in Hibernate

  12. 12

    instead of fetching multiple tables using pyspark how can we execute join query using jdbc

  13. 13

    JpaRepository native query not detecting parameters

  14. 14

    How to join using a nested column in Spark dataframe

  15. 15

    How to fetch all the batches for a query at one go using stored proc in azure SQL db?

  16. 16

    insert using sub-query and inner join

  17. 17

    Nested JSON fetch using jQuery

  18. 18

    SQL Query Update using a join

  19. 19

    Hibernate criteria using left join to filter results

  20. 20

    Create a double join query using Rails Admin

  21. 21

    how to change this sql query to hibernate query?

  22. 22

    Laravel 4.2 Eloquent using lists() with a join query

  23. 23

    How to fetch results from two tables using single query

  24. 24

    JPA or Hibernate or Spring annotation to specify a query for a relationship?

  25. 25

    Android: how to do an inner-join query in sqlite using the ContextResolver?

  26. 26

    Hibernate Join query to fetch the data in Java?

  27. 27

    Laravel join query using Laravel eloquent

  28. 28

    php how to fetch multiple records and display in a query

  29. 29

    How to exclude columns in hibernate join

ホットタグ

アーカイブ