JPA2:嵌套的主键类不能作为绑定null持久化

时间:2014-12-04 08:21:59

标签: java jpa eclipselink jpa-2.0 jpa-2.1

我正在使用JPA 2.x EclipseLink 2.5.1nested primary key class,如下所示: -

注意:幕后环境

1. OS: Windows 7 64 Bits
2. JDK: 1.7.0_65 64 Bits
3. Maven: 3.2.2
4. Arquillian: 1.1.5.Final
5. Container: Glassfish 4 embedded
6. Database: Derby 10.10.2.0 embedded

大师PK

@Embeddable
public class MasterPk {
    @Column(
        name   = "MASTER_ID",
        length = 5
    )
    private String masterId;

    //----> Setter/Getter is omitted
}

大师

@Entity
@Table(name = "MASTER_TAB")
public class Master {

    @EmbeddedId
    private MaskerPk id;

    @Column(
        name   = "MASTER_NAME",
        length = 40
    )
    private String masterName;

    @OneToMany(
        mappedBy = "master"
    )
    private List<Detail> details;

    //----> Setter/Getter is omitted
}

详情PK

@Embeddable
public class DetailPk {

    //----> THE NESTED IS HERE.
    @Embedded
    private MasterPk masterId;

    @Column(
        name   = "DETAIL_ID",
        length = 5
    )
    private String detailId;

    //----> Setter/Getter is omitted
}

细节

@Entity
@Table(name = "DETAIL_TAB")
public class Detail {

    @EmbeddedId
    private DetailPk id;

    @Column(
        name   = "DETAIL_NAME",
        length = 40
    )
    private String detailName;

    @MapsId("masterId")
    @ManyToOne
    @JoinColumns({
        @JoinColumn(
            name                 = "MASTER_ID",
            referencedColumnName = "MASTER_ID",
            nullable             = false
        )
    })
    private Master master;

    //----> Setter/Getter is omitted
}

持久化详细信息的编码

MasterPk masterPk = new MasterPk();
masterPk.setId("001"); //there is a 001 existed in the db

DetailPk detailPk = new DetailPk();
detailPk.setMasterId(masterPk);
detailPk.setDetailId("001"); //new detail to persist

Detail detail = new Detail();
detail.id(detailPk);
detail.setDetailName("detail-name");

em.persist(detail);

持久性XML

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
                   http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="unittest" transaction-type="JTA">

        <jta-data-source>jdbc/unittest</jta-data-source>

        <class>com.test.MasterPk</class>
        <class>com.test.Master</class>
        <class>com.test.DetiailPk</class>
        <class>com.test.Detail</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="eclipselink.ddl-generation" 
                      value="create-tables" />                     
            <property name="eclipselink.ddl-generation.output-mode"
                      value="both"/>
            <property name="eclipselink.application-location" 
                      value="target"/>                     
            <property name="eclipselink.create-ddl-jdbc-file-name" 
                      value="createDDL_ddlGeneration.jdbc"/>
            <property name="eclipselink.drop-ddl-jdbc-file-name" 
                      value="dropDDL_ddlGeneration.jdbc"/>
        </properties>

    </persistence-unit>
</persistence>

项目启动时,所有表格都已正确创建,尤其是primary keyforeign key。但是当persist带有编码时,由于Detail为空,因此Detail.masterId.id存在一些问题。 EclipseLink将我显示为

Column 'MASTER_ID' cannot accept a NULL value.

INSERT INTO DETAIL_TAB (DETAILE_NAME, DETAIL_ID, MASTER_ID)
VALUES (?, ?, ?)
bind => [detail-name, 001, null]

//
//----> We may be noticed that the masterId is not null. It is printed as 001.
//
Query: InsertObjectQuery(Detail(detailName=detail-name,
                                id=DetailPk(detailId=001,
                                            masterId=(MasterPk(masterId=001))
                                            )
                                )
                         )

我通过打印此field中的所有object-graph进行仔细检查,并确认masterId也不为空。我不确定是否做错了什么/误会。你能帮忙建议如何解决这个问题吗?

编辑1

  1. @ManyToOne修复为Detail.java。复制和粘贴到这个问题时这是一个错字
  2. 尝试将@Embedded添加到课程masterId的{​​{1}}。

    编辑1结果仍然面临同样的问题。

  3. 编辑2

    1. 添加用于创建DetailPkdetail
    2. 的编码
    3. 我直接创建persist而未找到MasterPk

      进一步的问题,我是否需要找到Master实体来获取Mater并将其分配给id而不是直接通过DetailPk创建?

1 个答案:

答案 0 :(得分:1)

由于MasterPK是可嵌入的,因此应使用@Embedded进行注释:

@Embeddable
public class DetailPk {

    @Embedded
    private MasterPk masterId;
    ...
}

我猜Detail实体也缺少@ManyToOne(或者它只是故意的?)

@Entity
@Table(name = "DETAIL_TAB")
public class Detail {
    ...
    @MapsId("masterId")
    @ManyToOne
    @JoinColumns({
        @JoinColumn(
            name                 = "MASTER_ID",
            referencedColumnName = "MASTER_ID",
            nullable             = false
        )
    })
    private Master master;
}

广告修改2

以下陈述

INSERT INTO DETAIL_TAB (DETAILE_NAME, DETAIL_ID, MASTER_ID)
VALUES (?, ?, ?)
bind => [detail-name, 001, null]

表示主 - 详细信息关系的拥有方(由@ManyToOne关联表示)为空。 从JPA的角度来看,这看起来如下:

  • MASTER_ID列与master实体
  • 中的Detail字段相关
  • Master实体使用MasterPK作为主键
  • Detail实体引用Mastermaster字段
  • 因为Detail实体充当关系的所有者,最好在其上指定级联,即@ManyToOne(cascade = CascadeType.PERSIST)

基于以上所述,为了使事情有效,我们需要设置拥有方,例如:

Master master = new Master();
master.id(masterPk);    

Detail detail = new Detail();
detail.id(detailPk);
detail.setMaster(master); // the owning side of the master-detail relationship

master.setDetail(detail); // mandatory if CascadeType.PERSIST is not defined
em.persist(detail);

这将导致:

INSERT INTO DETAIL_TAB (DETAIL_NAME, DETAIL_ID, MASTER_ID)
VALUES (?, ?, ?)
bind => [detail-name, 001, 001]