我有兩個班級名爲Banneers和BannerGroup。JPA + hibernate ...雙向關聯...而不是lnnerJoin,在查詢中發起左外連接
我想創建一個雙向關係。 在我的情況下,BannerGroup可以有許多橫幅和許多橫幅屬於同一個BannerGroup。
MySQL表是...
mysql> desc Banners;
+------------------+--------------+------+-----+---------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-----------------------------+
| banner_id | int(11) | NO | PRI | NULL | auto_increment |
| banner_name | varchar(255) | YES | | NULL | |
| banner_group_id | int(11) | YES | MUL | NULL | |
+------------------+--------------+------+-----+---------+-----------------------------+
mysql> desc Banner_Group;
+---------------------+--------------+------+-----+----------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+----------------+-----------------------------+
| group_id | int(11) | NO | PRI | NULL | auto_increment |
| group_name | varchar(255) | NO | MUL | NULL | |
+---------------------+--------------+------+-----+----------------+-----------------------------+
我的模型類是這樣的......
Class Banners
{
private Integer bannerId;
private String bannerName;
private BannerGroup bannerGroupDO;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "banner_id")
public Integer getBannerId() {
return bannerId;
}
public void setBannerId(Integer bannerId) {
this.bannerId = bannerId;
}
@Column(name = "banner_name")
public String getBannerName() {
return bannerName;
}
public void setBannerName(String bannerName) {
this.bannerName = bannerName;
}
@ManyToOne(fetch = FetchType.EAGER, optional=false)
@JoinColumn(name = "banner_group_id", nullable = false)
public BannerGroup getAffiliateBannerGroup() {
return bannerGroupDO;
}
public void setAffiliateBannerGroup(BannerGroup bannerGroupDO) {
this.bannerGroupDO = bannerGroupDO;
}
}
Class BannerGroup{
private int group_id;
private int group_name;
private List<Banners> banners;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "group_id")
public Integer getGroupId() {
return groupId;
}
public void setGroupId(Integer groupId) {
this.groupId = groupId;
}
@Column(name = "group_name")
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@OneToMany(mappedBy="bannerGroupDO", fetch = FetchType.LAZY)
public List<Banners> getBanners() {
return banners;
}
public void setBanners(List<Banners> banners) {
this.banners = banners;
}
}
現在,當我試圖讓列表這是懶洋洋地裝來自BannerGroup ...
Query q = session.createQuery("from com.abc.xyz.model.BannerGroup ");
我得到這個...
Query1: Hibernate: select b0_.group_id as group1_297_, b0_.group_name as group9_297_ from BANNER_GROUP b0_
Query2: Hibernate: select banner0_.banner_group_id as banner19_1_, banner0_.banner_id as banner1_1_, banner0_.banner_group_id as banner19_296_0_ from BANNERS banner0_ where banner0_.banner_group_id=?
這工作正常。
但現在,當我試圖讓BannerGroup這是熱切從橫幅加載中...
Query q = session.createQuery("from com.abc.xyz.model.Banners ab where ab.bannerGroupDO.groupId = :GroupId ");
我得到這個...
Hibernate: select b0_.banner_id as banner1_296_, b0_.banner_group_id as banner19_296_ from BANNERS b0_, BANNER_GROUP b1_ where b0_.banner_group_id=b1_.group_id and b0_.banner_group_id=?
Hibernate: select b0_.group_id as group1_297_1_, b0_.group_name as group9_297_1_, b1_.banner_group_id as banner19_3_, b1_.banner_id as banner1_3_, b1_.banner_group_id as banner19_296_0_ from BANNER_GROUP b0_ left outer join BANNERS b1_ on b0_.group_id=b1_.banner_group_id where b0_.group_id=?
我的問題是
1.) why "**left outer join**" ? not an "**inner join**"?
2.) If i want inner join for between Banners and BannerGroup. what should i do?
3.) Decision of join type is depend on what? JPA ? Hibernate? association? or anything else?
感謝kostja ...感謝明確的說明。 –
不客氣:) – kostja