Hibernate HQL join fetch not recursively fetching

Sotirios Delimanolis

I have the following query and method

private static final String FIND = "SELECT DISTINCT domain FROM Domain domain LEFT OUTER JOIN FETCH domain.operators LEFT OUTER JOIN FETCH domain.networkCodes WHERE domain.domainId = :domainId";

@Override
public Domain find(Long domainId) {
    Query query = getCurrentSession().createQuery(FIND);
    query.setLong("domainId", domainId);
    return (Domain) query.uniqueResult();
}

With Domain as

@Entity
@Table
public class Domain {
    @Id
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    @Column(name = "domain_id")
    private Long domainId;

    @Column(nullable = false, unique = true)
    @NotNull
    private String name;

    @Column(nullable = false)
    @NotNull
    @Enumerated(EnumType.STRING)
    private DomainType type;

    @OneToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    }, fetch = FetchType.EAGER)
    @JoinTable(joinColumns = {
            @JoinColumn(name = "domain_id")
    }, inverseJoinColumns = {
            @JoinColumn(name = "code")
    })
    @NotEmpty
    @Valid // needed to recur because we specify network codes when creating the domain
    private Set<NetworkCode> networkCodes = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(joinColumns = {
            @JoinColumn(name = "parent", referencedColumnName = "domain_id")
    }, inverseJoinColumns = {
            @JoinColumn(name = "child", referencedColumnName = "domain_id")
    })
    private Set<Domain> operators = new HashSet<>();
    // more
}

I would expect this single query to fetch the Set<NetworkCode> and Set<Domain> relations, but it doesn't. Say that the Domain I query has two operators, Hibernate would perform 1 + 2 * 2 = 5 queries

Hibernate: select distinct domain0_.domain_id as domain1_1_0_, domain2_.domain_id as domain1_1_1_, networkcod4_.code as code2_2_, domain0_.name as name1_0_, domain0_.type as type1_0_, domain2_.name as name1_1_, domain2_.type as type1_1_, operators1_.parent as parent1_0__, operators1_.child as child4_0__, networkcod3_.domain_id as domain1_1_1__, networkcod3_.code as code5_1__ from domain domain0_ left outer join domain_operators operators1_ on domain0_.domain_id=operators1_.parent left outer join domain domain2_ on operators1_.child=domain2_.domain_id inner join domain_network_codes networkcod3_ on domain0_.domain_id=networkcod3_.domain_id inner join network_code networkcod4_ on networkcod3_.code=networkcod4_.code where domain0_.domain_id=?
Hibernate: select operators0_.parent as parent1_1_, operators0_.child as child4_1_, domain1_.domain_id as domain1_1_0_, domain1_.name as name1_0_, domain1_.type as type1_0_ from domain_operators operators0_ inner join domain domain1_ on operators0_.child=domain1_.domain_id where operators0_.parent=?
Hibernate: select networkcod0_.domain_id as domain1_1_1_, networkcod0_.code as code5_1_, networkcod1_.code as code2_0_ from domain_network_codes networkcod0_ inner join network_code networkcod1_ on networkcod0_.code=networkcod1_.code where networkcod0_.domain_id=?
Hibernate: select operators0_.parent as parent1_1_, operators0_.child as child4_1_, domain1_.domain_id as domain1_1_0_, domain1_.name as name1_0_, domain1_.type as type1_0_ from domain_operators operators0_ inner join domain domain1_ on operators0_.child=domain1_.domain_id where operators0_.parent=?
Hibernate: select networkcod0_.domain_id as domain1_1_1_, networkcod0_.code as code5_1_, networkcod1_.code as code2_0_ from domain_network_codes networkcod0_ inner join network_code networkcod1_ on networkcod0_.code=networkcod1_.code where networkcod0_.domain_id=?

I'm guessing this is because I'm joining the operators Domain elements but they have to join themselves.

Is there an HQL query I can execute that would do both?

Andrei I

If you know that you have only two levels in your tree, have you thought of joining deeper one level. Something like below?

SELECT DISTINCT domain FROM Domain domain 
  LEFT OUTER JOIN FETCH domain.operators operators1 
  LEFT OUTER JOIN FETCH domain.networkCodes 
  LEFT OUTER JOIN FETCH operators1.operators operators2 
  LEFT OUTER JOIN FETCH operators1.networkCodes
WHERE domain.domainId = :domainId

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate fetch=join/select is not applicable for HQL?

From Dev

hibernate fetch lazy: Initialize() or HQL

From Dev

Hibernate HQL: Left Join with OnetoOne

From Dev

Hibernate chokes on this HQL cross join

From Dev

Hibernate HQL: is JOIN really necessary?

From Dev

NullPointerException with Hibernate FETCH JOIN

From Dev

Why is this HQL join fetch not returning expecting rows

From Dev

Hibernate HQl to join two level tables

From Dev

How to query join one to many in hibernate HQL

From Dev

Hibernate Fetch Mode Join Not working

From Dev

Alternative to fetch join with "ON clause" in Hibernate

From Dev

Nested fetch join with JPQL and Hibernate

From Dev

What is the difference between inner join and inner join fetch ? ,HQL

From Dev

How to join two tables in HQL to fetch results for one domain class

From Dev

Join fetch fails on fetching 3rd level relationship

From Dev

How to join fetch two associations with JPA/Hibernate

From Dev

Hibernate Fetch is not longer sub-type of Join

From Dev

Hibernate Join query to fetch the data in Java?

From Dev

Hibernate HQL bulk delete don't remove join table values

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

Hibernate LEFT JOIN HQL query not counting correctly with condition

From Dev

Hibernate HQL bulk delete don't remove join table values

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

Hibernate HQL from child to parent table using left join

From Dev

Fetching in Hibernate

From Java

What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

From Dev

mysqli fetch() not fetching

From Dev

What happens when we make fetch="join" and lazy="true" in hibernate

From Dev

Hibernate @OneToOne executes multiple queries even with @Fetch(FetchMode.JOIN)

Related Related

  1. 1

    Hibernate fetch=join/select is not applicable for HQL?

  2. 2

    hibernate fetch lazy: Initialize() or HQL

  3. 3

    Hibernate HQL: Left Join with OnetoOne

  4. 4

    Hibernate chokes on this HQL cross join

  5. 5

    Hibernate HQL: is JOIN really necessary?

  6. 6

    NullPointerException with Hibernate FETCH JOIN

  7. 7

    Why is this HQL join fetch not returning expecting rows

  8. 8

    Hibernate HQl to join two level tables

  9. 9

    How to query join one to many in hibernate HQL

  10. 10

    Hibernate Fetch Mode Join Not working

  11. 11

    Alternative to fetch join with "ON clause" in Hibernate

  12. 12

    Nested fetch join with JPQL and Hibernate

  13. 13

    What is the difference between inner join and inner join fetch ? ,HQL

  14. 14

    How to join two tables in HQL to fetch results for one domain class

  15. 15

    Join fetch fails on fetching 3rd level relationship

  16. 16

    How to join fetch two associations with JPA/Hibernate

  17. 17

    Hibernate Fetch is not longer sub-type of Join

  18. 18

    Hibernate Join query to fetch the data in Java?

  19. 19

    Hibernate HQL bulk delete don't remove join table values

  20. 20

    Hibernate: HQL join query on association doesnt result as expected

  21. 21

    Hibernate LEFT JOIN HQL query not counting correctly with condition

  22. 22

    Hibernate HQL bulk delete don't remove join table values

  23. 23

    Hibernate: HQL join query on association doesnt result as expected

  24. 24

    Hibernate HQL from child to parent table using left join

  25. 25

    Fetching in Hibernate

  26. 26

    What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

  27. 27

    mysqli fetch() not fetching

  28. 28

    What happens when we make fetch="join" and lazy="true" in hibernate

  29. 29

    Hibernate @OneToOne executes multiple queries even with @Fetch(FetchMode.JOIN)

HotTag

Archive