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