在 Play Framework 中使用 ebean 设置外键

Setting a Foreign Key using ebean in Play Framework

我有两个模型类

客户帐户

@Entity
public class CustomerAccount extends Model {
    @Id
    @Column(columnDefinition = "int(11)")
    public int customer_id;
    @Column(columnDefinition = "varchar(50) not null unique")
    public String customer_email;
    @Column(columnDefinition = "varchar(50) not null")
    public String password;
}

客户

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

现在我想在 table CustomerAccount 引用中添加一个外键给 Customer table,例如:-

外键(customer_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]]

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

客户

@Entity
public class Customer extends Model {

    @Id
    @Column(columnDefinition = "varchar(50) not null")
    public String email = "";

    public static Finder<String, Customer> find 
            = new Finder<>(String.class, Customer.class);

    @Column(columnDefinition = "varchar(50) not null")
    public String firstName;

    @Column(columnDefinition = "varchar(50) not null")
    public String lastName;

    @OneToMany(mappedBy = "customer")
    public List<CustomerAccount> accounts = new ArrayList<>();

}

客户帐户

@Entity
public class CustomerAccount extends Model {
    @Id
    public Integer id;

    public static Finder<Integer, CustomerAccount> find
            = new Finder<>(Integer.class, CustomerAccount.class);

    @ManyToOne()
    public Customer customer;

    @Column(columnDefinition = "varchar(50) not null")
    public String password;
}

它将生成 DDL:

create table customer (
  email                     varchar(50) not null not null,
  first_name                varchar(50) not null,
  last_name                 varchar(50) not null,
  constraint pk_customer primary key (email))
;

create table customer_account (
  id                        integer auto_increment not null,
  customer_email            varchar(50) not null,
  password                  varchar(50) not null,
  constraint pk_customer_account primary key (id))
;

alter table customer_account add constraint fk_customer_account_customer_1 foreign key (customer_email) references customer (email) on delete restrict on update restrict;
create index ix_customer_account_customer_1 on customer_account (customer_email);

顺便说一句。看一下注释 @OneToMany(mappedBy="customer") 它允许您获取客户的所有帐户而无需添加任何额外的数据库列,例如:

Customer customer = Customer.find.byId("foo@bar.com");

play.Logger.debug("All accounts of: " + customer.firstName);

for (CustomerAccount account : customer.accounts) {
    play.Logger.debug("ID: " + account.id);
}