2017-10-10 13 views
0

私のデータベースには、企業と従業員が含まれています。私は従業員を会社の弱い企業としてモデル化しました。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:

私のJPAのアノテーションは次のようになり

@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の主は、キー(id、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スクリプトを作成する方法はありますか?

EDIT:EmployeeSerializableはトリックをしない実装まかせ

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) 
+1

なぜこれが必要ですか? –

+1

「弱い実体」の意味は何ですか? –

+1

あなたが探しているのは、従業員の複合PKです。これはかなり標準的なもので、オプションは以下の通りです:https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys –

答えて

0

ちょうどあなたの@JoinColumn注釈下@Id注釈を追加します。しかし、Employeeクラスはimplements Serializableでなければなりません。

+0

残念ながら、それは動作しません。 エラーメッセージが追加されました。 – mollwitz

関連する問題