将Play Framework @ManyToOne关系链接到MySQL中的外键

时间:2012-09-12 15:06:20

标签: mysql jpa playframework foreign-keys playframework-1.x

我想做的是让我玩!项目使用MySQL上已有的数据库。与数据库的连接很好。我的问题在于将MySQL数据库中的外键约束链接到我的Play!项目的班级@ManyToOne关系。

所以基本上我现在拥有的是我的数据库:

...
create table clubadmin  (
    clubadmin_id  int not null auto_increment,
    club_id int not null,
    user_id int not null,
    primary key (clubadmin_id),
    constraint fk_clubadmin_club_id foreign key (club_id) references club(club_id), 
    constraint fk_clubadmin_user_id foreign key (user_id) references user(user_id)
);

create table special(
    spec_id int not null auto_increment,
    spec_datetime datetime not null,
    spec_content varchar(500) not null,
    clubadmin_id int not null,
    primary key (spec_id),
    constraint fk_special_clubadmin_id foreign key (clubadmin_id) references clubadmin(clubadmin_id)
);
...

强调特殊和俱乐部会员之间的关系。

在我的游戏中!项目我有一个ClubAdmin和一个特殊课程,具有以下内容:

package models;

...

@Entity
@Table(name="special")
public class Special extends GenericModel
{
        @Id 
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Required
        public Long spec_id;

        ...

        @ManyToOne
        @Required
        public Clubadmin clubadmin;

        public Special(Clubadmin admin,...)
        {
                ...
                this.clubadmin = admin;
                ...
        }
}

package models;

...

@Entity
@Table(name="clubadmin")
public class ClubAdmin extends GenericModel
{

        @Id 
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Required
        public Long clubadmin_id;

        ...

        @OneToMany(mappedBy="clubadmin",cascade=CascadeType.ALL)
        public List<Special> specials;

        public ClubAdmin(...)
        {
                ...
                this.specials=new ArrayList<Special>();
                ...
        }
}

从我所看到的,看起来在Special类中创建@ManyToOne关系是在Special表中创建一个新字段,其名称为clubadmin_clubadmin_id,表示外键,而不管存在fk_special_clubadmin_id约束。这不应该发生。

如何制作游戏!实现并使用数据库中的现有约束,以便它不创建自己的约束

我正在使用Play! 1.2.4和MySQL 5.3

谢谢!

1 个答案:

答案 0 :(得分:2)

将JoinColumn注释添加到ManyToOne字段,以指定不同于jpa推断的列名称

这样的东西
@JoinColumn(name="clubadmin_id", referencedColumnName="clubadmin_id")