0
我的數據庫包含公司和員工。我將員工建模爲公司的弱實體。使用JPA映射弱實體
我的JPA註釋是這樣的:
@Entity
public class Company extends Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
private List<Employee> employees;
}
Employee.java:
@Entity
public class Employee extends Model {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@ManyToOne(optional = false)
@JoinColumn(name="company_id", insertable=false, updatable=false)
private Company company;
}
下面的SQL代碼創建:
create table employee (
id bigint auto_increment not null,
company_id bigint not null,
constraint pk_employee primary key (id)
);
alter table employee add constraint fk_employee_company_id foreign key (company_id) references company (id) on delete restrict on update restrict;
我要的是(約束pk_employee主鍵(編號,company_id):
create table employee (
id bigint auto_increment not null,
company_id bigint not null,
constraint pk_employee primary key (id, company_id)
);
alter table employee add constraint fk_employee_company_id foreign key (company_id) references company (id) on delete restrict on update restrict;
有沒有辦法創建這樣的SQL腳本?
編輯: 讓Employee
實施Serializable
不會做的伎倆。
Caused by: javax.persistence.PersistenceException: Could not find BeanDescriptor for class models.Company. Perhaps the EmbeddedId class is not registered?
at io.ebeaninternal.server.deploy.BeanEmbeddedMetaFactory.create(BeanEmbeddedMetaFactory.java:26)
at io.ebeaninternal.server.deploy.BeanPropertyAssocOne.<init>(BeanPropertyAssocOne.java:79)
at io.ebeaninternal.server.deploy.BeanPropertyAssocOne.<init>(BeanPropertyAssocOne.java:62)
at io.ebeaninternal.server.deploy.meta.DeployBeanTable.createProperty(DeployBeanTable.java:68)
at io.ebeaninternal.server.deploy.meta.DeployBeanTable.createIdProperties(DeployBeanTable.java:59)
at io.ebeaninternal.server.deploy.BeanTable.<init>(BeanTable.java:42)
爲什麼你需要這個? –
「弱實體」是什麼意思? –
你正在尋找的是員工的綜合PK。這是相當標準的,並在這裏概述了選項:https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys –