在嵌入式类型中使用@OneToOne和Cascade.DELETE

时间:2013-06-12 09:26:20

标签: jpa eclipselink cascade one-to-one

在应用程序中,我将EclipseLink 2.4.1与Java Persistence 2.0.4一起使用。

我在嵌入式类中有一个OneToOne映射。一切正常,除了删除。当我尝试删除包含嵌入类的对象时,会发生以下异常。我检查了我没有在我自己的嵌入对象上调用删除代码中的某处。有谁知道如何避免这个错误或如何绕过它?

Exception [EclipseLink-6002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.QueryException
Exception Description: Aggregated objects cannot be written/deleted/queried independently from their owners. 
Descriptor: [RelationalDescriptor(org.openlca.web.model.ProcessModelInfo --> [])]
Query: DeleteObjectQuery(org.openlca.web.model.ProcessModelInfo@77cc2975)
    at org.eclipse.persistence.exceptions.QueryException.aggregateObjectCannotBeDeletedOrWritten(QueryException.java:240)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.prepare(ObjectLevelModifyQuery.java:205)
    at org.eclipse.persistence.queries.DeleteObjectQuery.prepare(DeleteObjectQuery.java:327)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:614)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:575)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:820)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:751)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.DeleteObjectQuery.executeInUnitOfWorkObjectLevelModifyQuery(DeleteObjectQuery.java:119)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1535)
    at org.eclipse.persistence.queries.DeleteObjectQuery.executeDatabaseQuery(DeleteObjectQuery.java:194)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:751)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.DeleteObjectQuery.executeInUnitOfWorkObjectLevelModifyQuery(DeleteObjectQuery.java:119)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1535)
    at org.eclipse.persistence.internal.sessions.CommitManager.deleteAllObjects(CommitManager.java:334)
    at org.eclipse.persistence.internal.sessions.CommitManager.deleteAllObjects(CommitManager.java:288)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1422)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:634)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1509)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:266)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1147)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
    at org.project.ProcessDao.delete(ProcessDao.java:41)

相关的类片段看起来像这样(在persistence.xml中添加了Process和LongText) - 尝试删除进程时发生错误:

实体类流程

@Entity
public class Process {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "process_seq")
    @Column(name = "id")
    private long id;

    ....

    @Embedded
    private ProcessModelInfo modelInfo = new ProcessModelInfo();

    ....

} 

嵌入式类ProcessModelInfo

@Embeddable
public class ProcessModelInfo {

    ...

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name= "f_modelling_constants")
    private LongText modellingConstants = new LongText();

    ...

}

实体类LongText

@Entity
@Table(name = "tbl_long_texts")
public class LongText {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "long_text_seq")
    @Column(name = "id")
    private long id;

    @Lob
    @Column(name = "text")
    private String text;

    ....

}

ProcessDao.delete方法如下所示:

@Override
public void delete(Process entity) throws Exception {
    if (entity == null)
        return;
    EntityManager em = createManager();
    try {
        em.getTransaction().begin();
        em.remove(em.merge(entity));
        em.getTransaction().commit();
    } finally {
        em.close();
    }

}

1 个答案:

答案 0 :(得分:0)

我看不出会发生这种情况,但如果您可以创建可重现的测试用例,请记录错误。

检查您是否在embeddable上没有任何可能被删除的事件。 尝试调试或将日志记录级别设置为最佳。

您可能想尝试2.5版本,因为它可能已修复(虽然我没有看到代码中的任何更改)。