EclipseLink - Weblogic 12 - 使用双向映射插入订单问题

时间:2016-01-22 10:24:16

标签: jpa inheritance eclipselink bidirectional

我在Weblogic 12上使用EclipseLink(JPA2)时遇到了问题。 我在互联网上搜索了一个解决方案但是在花了太多时间没有找到修复后,我决定请求你体验:也许你们中的一个可以帮助我。

(此映射在WL 10.3.3 - EclipseLink(JPA1))上完美运行

基本用户故​​事:任何'电源'应用程序的用户(称为'雇主')能够对其他用户发出一些请求(更改)。你可以考虑一些雇主' (人力资源部,经理)可以要求对其他组织的员工进行一些更改(地址变更,从部门转移到另一个部门(或组建团队到另一个团队),雇用新员工,解雇雇员等等。 ..

我的结构如下:

任何类型的请求'继承自REQUEST(一个对象和一个表 - 它包含所有请求王的PK)。例如:HiringRequest,EmployerModificationRequest,FiringRequest,TransferRequest(具有子类/子类型)。

@Entity
@Table(name = "TRANSFER_REQUEST")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@PrimaryKeyJoinColumn(name = "REQUEST_ID")
public abstract class TransferRequest extends Request {

    private static final long serialVersionUID = 1L;

    ...
    @OneToMany(cascade = ALL, mappedBy = "transferRequest")
    private List<EmployeeInTransfer> employees;


    public List<EmployeeInTransfer> getSourceEmployers() {
        return employees;
    }

    public void setSourceEmployers(List<EmployeeInTransfer> inEmployees) {
        for (EmployeeInTransfer employee : inEmployees) {
            employee.setTransferRequest(this);
        }
        this.employees = inEmployees;
    }
    ...
}
@Entity
@DiscriminatorValue("TEAM_CHANGE")
public class TeamChangeRequest extends TransferRequest {

    private static final long serialVersionUID = 5747883857562212522L;

    ...

}
@Entity
@Table(name = "EMPLOYEE_IN_TRANSFER")
@SequenceGenerator(name = "EMPLOYEE_IN_TRANSFER_SEQ", sequenceName = "Employee_In_Transfer_Sequence", allocationSize = 1, initialValue = 1)
public class EmployeeInTransfer implements Serializable {

    private static final long serialVersionUID = 8987161385291888922L;

    @Id
    @Column(name = "EMPLOYEE_IN_TRANSFER_ID", nullable = false, length = 16)
    @GeneratedValue(generator = "EMPLOYEE_IN_TRANSFER_SEQ")
    private Long id;

    @ManyToOne(optional = false, fetch=LAZY)
    @JoinColumn(name = "TRANSFER_REQUEST_ID", nullable=false)
    private TransferRequest transferRequest;

    ...
}

EmployerIntransfer

@Test
    public void teamChangePersistenceTest() {

        DateTime creationOrReceptionDate = toDateTime(getDate(2010, 5, 5)).minusDays(5);

        TransferRequest transferRequest = aTeamChangeRequest()
            .withCreationDateTime(creationOrReceptionDate.toDate())
            .build();
        em.persist(transferRequest);

        em.flush();  
    }

测试持久性的测试类:

...
[EL Fine]: sql: 2016-01-22 11:02:06.962--INSERT INTO REQUEST ...
[EL Fine]: sql: 2016-01-22 11:02:06.986--INSERT INTO CATEGORY ...
[EL Fine]: sql: 2016-01-22 11:02:06.995--INSERT INTO EMPLOYEE_IN_TRANSFER ...
[EL Fine]: sql: 2016-01-22 11:02:07.041--SELECT 1 FROM DUAL
[EL Warning]: 2016-01-22 11:02:07.05--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: ORA-02291: integrity constraint (MYSCHEMA.FK_EIT_TR) violated - parent key not found

日志显示:

[^\\]*

所以... 如果禁用了约束,则TeamChangeRequest的持久性发生在EmployerInTransfer Objets之后,一切都很好。 如果约束已启用(并且应该是正确的情况),则由于插入语句的顺序错误,我面临约束违规。

Request是TransferRequest(TeamChangeRequest)的父级,它负责与EmployeeInTransfer(child)的关系(父级)。

我如何调整EclipseLink以确保在任何EmployeeInTransfer语句之前插入TeamChangerequest对象?

你有什么线索我能解决吗?

1 个答案:

答案 0 :(得分:1)

由于继承,Request实体是具有主键的实体; JPA限制实体仅引用主键,EclipseLink相应地优化了插入。

这意味着带有连接列“TRANSFER_REQUEST_ID”的EmployeeInTransfer transferRequest关系应该对REQUEST表有一个约束,而不是“TRANSFER_REQUEST”表。

也就是说,您可以告诉EclipseLink特别是它在连接列中加入哪个字段,而不是让它默认为REQUEST表。指定包含referencedColumnName的表,而不是默认值:

@ManyToOne(optional = false, fetch=LAZY)
@JoinColumn(name = "TRANSFER_REQUEST_ID", referencedColumnName="TRANSFER_REQUEST.REQUEST_ID", nullable=false)
private TransferRequest transferRequest;