JPA插入问题 - 带有EmbeddedId的OneToMany / ManyToOne

时间:2015-07-13 19:00:17

标签: spring jpa orm eclipselink

我遇到一个问题,即父项生成多个插入语句,因此在保存子项时遇到了挑战。代码:

@Entity
@Table(name="PARENT")
public class Parent implements Serializable {
        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private ParentPK id;

        @Column(name = "FIRST_NAME")
        private String firstName;

        @Column(name = "LAST_NAME")
        private String lastName;

    // bi-directional many-to-one association to ParDepend
    @OneToMany(fetch=FetchType.EAGER, mappedBy="parent", cascade={CascadeType.ALL}, orphanRemoval=true)
    private List<ParDepend> parDepends;

    //getters, setters, add (for parDepends), remove (for parDepends)

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Borrower other = (Borrower) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

@Embeddable
public class ParentPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @SequenceGenerator(name = "parSeq", sequenceName = "PARENT_SEQ", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parSeq")
    @Column(name="PAR_SK_SEQ")
    private long parSkSeq;

    @Column(name="PAR_REV_NUM")
    private long parRevNum;

    //getter, setters

    //JPA generated equals and hashcode
}

@Entity
@Table(name="PAR_DEPEND")
public class ParDepend implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ParDependPK id;

    private BigDecimal age;

    //bi-directional many-to-one association to Parent
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name="PAR_REV_NUM", referencedColumnName="PAR_REV_NUM"),
        @JoinColumn(name="PAR_SK_SEQ", referencedColumnName="PAR_SK_SEQ")
        })
    private Parent parent;

    //getters, setters

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BorrDepend other = (BorrDepend) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}

@Embeddable
public class ParDependPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="PAR_SK_SEQ", insertable=false, updatable=false, nullable=false)
    private long parSkSeq;

    @Column(name="PAR_REV_NUM", insertable=false, updatable=false, nullable=false)
    private long parRevNum;

    @SequenceGenerator(name = "parDependSeq", sequenceName = "PAR_DEPEND_SEQ", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parDependSeq")
    @Column(name="SEQ_NUM")
    private long seqNum;

    @Column(name="REV_NUM")
    private long revNum;

    //getter setter

    //JPA generated equals and hashcode
}

我正在使用dozer将UI返回的数据转换为JPA实体映射。然后使用Spring Repository来保存和刷新Borrower实体。它正在生成借用者序列两次并为借用者插入空值,而不是实际值。我的假设是实际值在内存中插入第一个序列,而第二个序列将null数据插入。有关JPA映射问题的任何建议吗?我没有看到推土机是一个问题,因为日志显示JPA实体是创建为

的属性

{parent = Parent [id = ParentPK [parSkSeq = 0,parRevNum = 0],firstName = ABCD,lastName = XYZ,parDepends = [ParDepend [id = ParDependPK [parSkSeq = 0,parRevNum = 0,seqNum = 0, revNum = 0],年龄= 31]]]}

JPA Ecliselink版本2.5.0-RC1 Spring Data JPA框架版本1.7.1.RELEASE

请告诉我此问题所需的任何其他信息。

编辑:更新推土机日志。

0 个答案:

没有答案