-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate JOIN or Secondary Query Fetch policy explanation
PostPosted: Thu Aug 31, 2017 5:20 am 
Newbie

Joined: Tue Apr 25, 2017 12:26 am
Posts: 4
I have two table and there ManyToOne Join with fetch Eager.
But Still Hibernate is firing single query instead of JOIN.

How does hibernate decide when to use JOIN and when use single query?

I mean even if we use LAZY, sometimes hibernate fires JOIN query instead of single queries.
With Eager, it should fire JOIN but sometimes fires single query.

This is I am not able to understand?

Table 1
Code:
@Entity
@Table(name="localityCityMapping")
public class LocalityCityMapping implements Serializable {
    private static final long serialVersionUID = 1L;

@Id
private int localityId;

private int cityId;

private String localityName;

private String status;

private Timestamp submitDate;

private int zoneId;

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="cityId", referencedColumnName="city_id", insertable=false, updatable=false)
private CountryCityTable countryCityData;


Table 2

Code:
@Entity
@Table(name="countryCityTable")
public class CountryCityTable implements Serializable {
    private static final long serialVersionUID = 1L;

@Id
@Column(name="city_id")
private int cityId;

private int addedBy;

@Column(name="city_name")
private String cityName;

private int countryId;

private Timestamp creationDate;

private int enabled;

private String notes;


@Column(name="state_id")
private int stateId;

private byte tier;

}



Same queries even if Join is EAGER / LAZY ?

Code:
Hibernate: select localityci0_.localityId as locality1_2_, localityci0_.cityId as cityId2_2_, localityci0_.localityName as locality3_2_, localityci0_.status as status4_2_, localityci0_.submitDate as submitDa5_2_, localityci0_.zoneId as zoneId6_2_ from localityCityMapping localityci0_ where localityci0_.localityId in (? , ?)

Hibernate: select countrycit0_.city_id as city_id1_0_0_, countrycit0_.addedBy as addedBy2_0_0_, countrycit0_.city_name as city_nam3_0_0_, countrycit0_.countryId as countryI4_0_0_, countrycit0_.creationDate as creation5_0_0_, countrycit0_.enabled as enabled6_0_0_, countrycit0_.notes as notes7_0_0_, countrycit0_.state_id as state_id8_0_0_, countrycit0_.tier as tier9_0_0_ from countryCityTable countrycit0_ where countrycit0_.city_id=?


Top
 Profile  
 
 Post subject: Re: When Hibernate use JOIN and when Single Queries
PostPosted: Thu Aug 31, 2017 5:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Code:
I have two table and there ManyToOne Join with fetch Eager.


Bad idea! EAGER fetching is, in the best case, a code smell, although usually you can regard it as an Anti-Pattern.

Quote:
How does Hibernate decide when to use JOIN and when using single query?


1. If you do a direct fetch via an entityManager.find call, then a JOIN will be used
2. If you execute a JPQL but you forget to JOIN FETCH, a secondary query will be issued

Now, the 2nd example can easily lead to N+1 query issues which is another reason you should not use EAGER fetching.

Now, in your example you are using LAZY, so the only time you'll see a JOIN is if you use JOIN in your JPQL query.

For LAZY fetching, check out this article about the best way to avoid the LazyInitializationException.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.