I have two model classes
@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 = "";
}
Now I want to add a foriegn key in table CustomerAccount reference to Customer table like: -
Foreign Key (customer_email) references Customer(email);
Please tell me how to do this...
while running the following code to add details to Customer Account
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");
}
}
I am getting this error
[PersistenceException: ERROR executing DML bindLog[] error[Column 'customer_email' cannot be null]]
Ebean uses @Id
field as foreign key by default, so your models would need look like:
Customer
@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;
}
it will generate the 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);
BTW. take a look at annotation @OneToMany(mappedBy="customer")
it allows you to fetch all accounts of customer without adding any additional DB columns like:
Customer customer = Customer.find.byId("[email protected]");
play.Logger.debug("All accounts of: " + customer.firstName);
for (CustomerAccount account : customer.accounts) {
play.Logger.debug("ID: " + account.id);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With