Reputation: 223
My database contains a companies and employees. I have modeled Employee as a weak entity of Company.
My JPA annotations look like:
@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;
}
The following SQL code is created:
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;
What I want is (constraint pk_employee primary 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;
Is there a way to create such SQL script?
EDIT:
Letting Employee
implement Serializable
does not do the trick.
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)
Upvotes: 3
Views: 4936
Reputation: 66
Thanks to @Alan Hay for giving us the link https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys
Here is how you create a weak entity Address that takes it's Id (user_id) from the table pmo_user
pmo_user{id, ....}
test_address{user_id, address}
============================
@Entity
@Table(name="test_address")
public class Address
{
@Id
@Column(name="user_id")
protected int userId;
@OneToOne
@PrimaryKeyJoinColumn(name="user_id", referencedColumnName="id")
protected PmoUser owner;
@Column(name="address")
protected String address;
public void setOwner(PmoUser owner)
{
this.owner = owner;
this.userId = owner.getId();
}
@Override
public String toString() {
return "Address [userId=" + userId + ", owner=" + owner + ", address=" + address + "]";
}
}
Upvotes: 2
Reputation: 2323
Just add @Id
annotation under your @JoinColumn
annotation. But Employee
class must implements Serializable
to use this solution.
Upvotes: 0