我有两个表,还有ManyToOne和fetch Eager一起加入。 但仍然Hibernate正在触发单个查询而不是JOIN。
hibernate如何决定何时使用JOIN以及何时使用单个查询?
我的意思是即使我们使用LAZY,有时候hibernate会激活JOIN查询而不是单个查询。 使用Eager,它应该激活JOIN,但有时会触发单个查询。
这是我无法理解的?
表1
@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;
...
}
表2
@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;
}
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=?
相同的查询即使加入是EAGER / LAZY。