1

我有两个名为 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'.

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

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 关系中不起作用。

我的代码错了吗?这里有什么问题?

4

1 回答 1

2

why do you have bannerGroupId and bannerGroupDO? Just have bannerGroupDO and on the getter have

some thing like

    @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "banner_group_id", nullable = false)
public Node getBannerGroupDO() {
    return this.bannerGroupDO;
}
于 2013-10-01T04:36:16.897 回答