Playframework 使用ebean in-Play框架设置外键

Playframework 使用ebean in-Play框架设置外键,playframework,foreign-key-relationship,ebean,Playframework,Foreign Key Relationship,Ebean,我有两个模型课 客户账号 顾客 现在,我想在CustomerAccount表引用中添加一个外键到Customer表,如下所示:- 外键(客户\电子邮件)引用客户(电子邮件) 请告诉我怎么做 运行以下代码向客户帐户添加详细信息时 public static Result addPerson() { String result = "ok"; CustomerAccount Customer_Account = Form.form(CustomerAccount.c

我有两个模型课

客户账号 顾客 现在,我想在CustomerAccount表引用中添加一个外键到Customer表,如下所示:-

外键(客户\电子邮件)引用客户(电子邮件)

请告诉我怎么做


运行以下代码向客户帐户添加详细信息时

public static Result addPerson() {
        String result = "ok";
        CustomerAccount Customer_Account = Form.form(CustomerAccount.class).bindFromRequest().get();
        List<CustomerAccount> personsDetails = new Model.Finder(String.class, CustomerAccount.class).all();

        for (CustomerAccount product : personsDetails) {
            if (product.customer.email.equals(Customer_Account.customer.email) ) {
                result = "";
            }
        }
        if (result.equals("ok")) {
            Customer_Account.save();
            return ok("New Customer Account Created");
        }else{
            return ok("Customer with same email Already Exists");
        }

    }

默认情况下,Ebean使用
@Id
字段作为外键,因此您的模型需要如下所示:

客户

@实体
公共类客户扩展模型{
@身份证
@列(columnDefinition=“varchar(50)非空”)
公共字符串email=“”;
公共静态查找器查找
=新查找器(String.class、Customer.class);
@列(columnDefinition=“varchar(50)非空”)
公共字符串名;
@列(columnDefinition=“varchar(50)非空”)
公共字符串lastName;
@OneToMany(mappedBy=“客户”)
public List accounts=new ArrayList();
}
客户账户

@实体
公共类CustomerAccount扩展模型{
@身份证
公共整数id;
公共静态查找器查找
=新查找程序(Integer.class、CustomerAccount.class);
@ManyToOne()
公众客户;
@列(columnDefinition=“varchar(50)非空”)
公共字符串密码;
}
它将生成DDL:

创建客户表(
电子邮件varchar(50)不为空不为空,
first_name varchar(50)不为空,
姓氏varchar(50)不为空,
约束pk_客户主键(电子邮件))
;
创建表customer\u帐户(
id整数自动增量不为空,
客户电子邮件varchar(50)不为空,
密码varchar(50)不为空,
约束主键\客户\帐户主键(id))
;
更改表customer\u account添加约束fk\u customer\u account\u customer\u 1外键(customer\u email)在删除时引用customer(email)限制在更新时限制;
在客户账户(客户电子邮件)上创建索引ix客户账户1;
顺便说一句,看看annotation
@OneToMany(mappedBy=“customer”)
它允许您获取客户的所有帐户,而无需添加任何其他DB列,如:

Customer=Customer.find.byId(“foo@bar.com");
play.Logger.debug(“所有帐户:“+customer.firstName”);
for(CustomerAccount帐户:customer.accounts){
play.Logger.debug(“ID:+account.ID”);
}

您是否尝试过文档中使用的任何注释:。如果是这样的话,最好能清楚地说明它们对您不起作用的地方。我不知道如何实现这一点。我已经开始学习游戏。我改变了主意;)您不能将
Customer.field
设置为唯一,因为无法正确使用
manytone
注释,反正它是主键,所以数据库确保其唯一性。检查最新编辑,我对其进行了一点修复,并在Customer上添加了关系,使其具有双向性
@Entity
public class Customer extends Model {

    @Column(columnDefinition = "int(11) unique")
    public int customer_id = 6;
    @Column(columnDefinition = "varchar(50) not null")
    public String customer_fname;
    @Column(columnDefinition = "varchar(50) not null")
    public String customer_lname;
    @Column(columnDefinition = "varchar(50) unique not null")
    public String email = "";
}
public static Result addPerson() {
        String result = "ok";
        CustomerAccount Customer_Account = Form.form(CustomerAccount.class).bindFromRequest().get();
        List<CustomerAccount> personsDetails = new Model.Finder(String.class, CustomerAccount.class).all();

        for (CustomerAccount product : personsDetails) {
            if (product.customer.email.equals(Customer_Account.customer.email) ) {
                result = "";
            }
        }
        if (result.equals("ok")) {
            Customer_Account.save();
            return ok("New Customer Account Created");
        }else{
            return ok("Customer with same email Already Exists");
        }

    }
[PersistenceException: ERROR executing DML bindLog[] error[Column 'customer_email' cannot be null]]