2013-10-22 65 views
1

我有兩個班級名爲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? 

回答

1

您正在獲取外連接,因爲您已指定獲取所有BannerGroup實體。

如果持久性提供程序使用內部聯接,則不會提取所有BannerGroup未使用Banners關聯的實體。

這behavoir是查詢 from com.abc.xyz.model.BannerGroup,如果你想要一個明確的內部連接,適應您的查詢這是select b from BannerGroup

形式的JPA符合要求的解釋:

select b from BannerGroup join b.banners

inner關鍵字是可選的,所以這種查詢是等價的:

select b from BannerGroup inner join b.banners

+0

感謝kostja ...感謝明確的說明。 –

+0

不客氣:) – kostja