JPA:在ManyToOne关联期间,选择查询不起作用

时间:2013-10-01 04:29:24

标签: hibernate jpa model many-to-one

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

我想创建关系 ManyToOne

在我的情况下,许多横幅可以拥有相同的横幅广告。

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 Integer bannerGroupId;
    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;
    }

    @Column(name = "banner_group_id")
    public Integer getBannerGroupId() {
        return bannerGroupId;
    }

    public void setBannerGroupId(Integer bannerGroupId) {
        this.bannerGroupId = bannerGroupId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id")
    public AffiliateBannerGroup getAffiliateBannerGroupDO() {
        return affiliateBannerGroupDO;
    }

    public void setAffiliateBannerGroupDO(
            AffiliateBannerGroup affiliateBannerGroupDO) {
        this.affiliateBannerGroupDO = affiliateBannerGroupDO;
    }
}

Class BannerGroup{

    private int group_id;
    private int group_name;

    @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;
    }
}

现在,当我想要这样做时......

Query q = session.createQuery("from com.abc.xyz.model.Banners ");
List<Banners> bannerDisplayList = q.list();

它给了我以下例外......

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'b0_.group_id' in 'field list'.

当我看到我的genral查询日志时,它给了我这个......

131001  9:42:51  230 Query      select b0_.banner_id as banner1_247_, b0_.group_id as group25_247_, b0_.banner_group_id as banner3_247_, b0_.banner_name as banner2_247_ from Banners b0_
                  230 Query     SHOW FULL TABLES FROM `mydb` LIKE 'PROBABLYNOT'
                  230 Query     rollback
                  230 Query     SET autocommit=1

这意味着 @joinColumn 在此ManyToOne关系中无效。

我的代码错了吗? 这有什么不对?

1 个答案:

答案 0 :(得分:2)

为什么你有bannerGroupId和bannerGroupDO?只需拥有bannerGroupDO并且在getter上有

有点像

    @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "banner_group_id", nullable = false)
public Node getBannerGroupDO() {
    return this.bannerGroupDO;
}