即使没有Cascade.PERSIST,eclipseLink也会在manyToOne关系中保存新对象

时间:2014-07-03 05:19:41

标签: jpa eclipselink

我有这种情况

public class A {

    @ManyToOne(cascade = { CascadeType.REFRESH })
    @JoinColumn(name = "COLUMN_B_ID", insertable = false, updatable = false)
    private B testB;

}

我认为无论对A类中的属性testB进行了哪些更改,它都不会被持久化。但是,当我执行以下操作时

A testA = new A();
a.setTestB(new B());
save(testA);

eclipseLink尝试在持久化testA之前先保留testB。我认为,由于testB上的ManyToOne映射未设置为CascadeType.PERSIST,并且由于insertable和updateable为false,因此在保存A时,任何在A上设置B的新实例的尝试都不起作用,但我看到相反的情况。知道什么可能是错的吗?有办法防止这种情况吗?

1 个答案:

答案 0 :(得分:0)

我正在针对Eclipselink 2.2.1测试下面的代码,它会抛出一个要求CascadeType.PERSIST存在的异常:

Caused by: java.lang.IllegalStateException: During synchronization a new object
was found through a relationship that was not marked cascade PERSIST: eclipselin
k.saving.neww.objects.in.manytoone.relationship.even.when.no.cascade.B@109f188a.

使用Eclipselink 2.5.1有相同的例外:

Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: test.B@6db66c18.
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:310)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:723)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1516)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:277)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
A a = new A();
a.setTestB(new B());
em.persist(a);