簡體   English   中英

使用JPA映射弱實體

[英]Mapping a weak entity with JPA

我的數據庫包含公司和員工。 我已將Employee建模為Company的弱勢實體。

我的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主鍵(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腳本?

編輯: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)

只需在@JoinColumn注釋下添加@Id注釋。 但是Employee類必須implements Serializable才能使用此解決方案。

感謝@Alan Hay為我們提供了鏈接https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys

這是創建弱實體地址的方法,該地址從表pmo_user中獲取其ID(user_id)

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 + "]";
    }    

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM