【问题标题】:Setting a Foreign Key using ebean in Play Framework在 Play Framework 中使用 ebean 设置外键
【发布时间】:2015-05-13 11:37:01
【问题描述】:

我有两个模型类

客户帐户

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

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

外键(customer_email)引用客户(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]]

【问题讨论】:

标签: playframework foreign-key-relationship ebean


【解决方案1】:

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

【讨论】:

  • 改变了主意 ;) 不能将Customer.field 设置为唯一,因为ManyToOne 注释将无法正确使用,反正它是主键,所以数据库保证它的唯一性。跨度>
  • 检查最新的编辑,我对其进行了一些修复,并在客户上添加了关系以使其成为双向
猜你喜欢
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多