2017-10-10 3 views
0

Ma base de données contient des sociétés et des employés. J'ai modélisé Employé comme une entité faible de la Société.Mappage d'une entité faible avec JPA

Mes annotations JPA ressemblent:

@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; 
} 

Le code SQL suivant est créé:

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; 

Ce que je veux est (contrainte primaire pk_employee key (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; 

Existe-t-il un moyen de créer un tel script SQL?

EDIT: Laisser Employee mettre en œuvre Serializable ne pas faire l'affaire.

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

Pourquoi devriez-vous en avoir besoin? –

+1

Quelle est la signification de "entité faible"? –

+1

Ce que vous cherchez est un Composite PK on Employee. Ceci est assez standard et les options sont décrites ici: https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys –

Répondre

0

Il suffit d'ajouter @Id annotation sous votre annotation @JoinColumn. Mais la classe Employee doit utiliser implements Serializable pour utiliser cette solution.

+0

Malheureusement, cela ne fonctionne pas. J'ai ajouté le message d'erreur. – mollwitz