gpt4 book ai didi

playframework - 在 Play Framework 中使用 ebean 设置外键

转载 作者:行者123 更新时间:2023-12-04 19:03:48 35 4
gpt4 key购买 nike

我有两个模型类

客户账户

@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) 引用 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);
}

关于playframework - 在 Play Framework 中使用 ebean 设置外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213670/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com