JPA + hibernate ...双向关联...在查询中触发左外连接而不是lnnerJoin

时间:2013-10-22 06:44:53

标签: hibernate jpa associations

我有两个名为Banneers和BannerGroup的课程。

我想创建双向关系。 在我的例子中,A 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=?

这样可以。

但是现在,当我试图从横幅加载横向集团 急切时......

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 个答案:

答案 0 :(得分:1)

您正在获取外部联接,因为您已指定要提取所有 BannerGroup实体。

如果持久性提供程序使用内部联接,则不会获取没有BannerGroup关联的所有Banners实体。

此行为是对查询的符合JPA的解释 from com.abc.xyz.model.BannerGroupselect b from BannerGroup

的形式

如果您想要明确的内部联接,请调整您的查询:

select b from BannerGroup join b.banners

inner关键字是可选的,因此此查询是等效的:

select b from BannerGroup inner join b.banners