使用JPA映射弱实体

时间:2017-10-10 01:41:17

标签: java hibernate 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主键(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)

2 个答案:

答案 0 :(得分:0)

只需在T...注释下添加@Id注释即可。但是@JoinColumn类必须Employee才能使用此解决方案。

答案 1 :(得分:0)

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

}