我有两个模型类
if ([3,5,7].include?a and b == 2 and [5,6].include?c and [8,9,0].include?d)
@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;
}
现在我想在表CustomerAccount引用中添加一个foriegn键,如: -
外键(customer_email)引用客户(电子邮件);
请告诉我怎么做......
运行以下代码以向客户帐户添加详细信息
@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");
}
}
答案 0 :(得分:5)
默认情况下,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<>();
}
<强> CustomerAccount 强>
@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);
}