1

使用:Glassfish 3.1.2、EclipseLink。

我有以下三类 JPA 模型:

@Entity public class Customer implements Serializable {

@Id private Integer id;

@OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval=true)
private Person person;

[...]

@Entity public class Person implements Serializable {

@Id private Integer id;

[...]

@Entity public class Request implements Serializable {

@Id private Integer id;

@ManyToOne private Person person;

我尝试使用以下策略(使用CMT)删除客户:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.ddl-generation" value="create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="database"/>

        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.logging.parameters" value="true"/>
        <property name="eclipselink.logging.logger" value="DefaultLogger"/>                  
        <property name="eclipselink.logging.timestamp" value="true"/>
        <property name="eclipselink.logging.session" value="false"/>
        <property name="eclipselink.logging.thread" value="false"/>
    </properties>
</persistence-unit>

[...]

@PersistenceContext(unitName="MyPU")
private EntityManager entityManager;   

@Resource private SessionContext context;

[...]

public void delete(Entity object) {

    try{

        object = this.getEntityManager().merge(object);
        this.getEntityManager().remove(object);

    } catch (Exception e){

        this.context.setRollbackOnly();
    }
}

Customer对象附加到附加到Request的Person对象时,Person的删除级联失败导致事务回滚,但Customer会从数据库中删除。我收到以下错误:

INFO: [EL Fine]: 2012-12-28 10:53:38.1--Connection(27132168)--DELETE FROM CUSTOMER WHERE (ID = ?)
bind => [97]
INFO: [EL Fine]: 2012-12-28 10:53:38.125--Connection(27132168)--DELETE FROM PERSON WHERE (ID = ?)
bind => [111]
INFO: [EL Fine]: 2012-12-28 10:53:38.126--SELECT 1
WARNING: DTX5014: Caught exception in beforeCompletion() callback:
Local Exception Stack:
INFO: [EL Warning]: 2012-12-28 10:53:38.127--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: atualização ou exclusão em tabela "person" viola restrição de chave estrangeira "fk_request_person_id" em "request"
Detalhe: Chave (id)=(111) ainda é referenciada pela tabela "request".
Error Code: 0
Call: DELETE FROM PERSON WHERE (ID = ?)
bind => [111]
Query: DeleteObjectQuery(111)
[...]
SEVERE: javax.ejb.EJBException: Transaction aborted
[...]

那么,当级联删除失败时,如何取消客户删除?

4

1 回答 1

0

这里有两件事可能会出错。

  • 未正确指定事务边界

可能由于这个原因,您的应用程序服务器没有正确发出 BEGIN 语句。这可以解释 Postgres 有问题,而 Oracle 没有(它隐式启动事务)。确保您的服务方法带有正确的注释。如果一切都好,也许

  • 您的数据源有问题。

它是 JTA 兼容的数据源吗?它是否使用正确的 Postgres 驱动程序?请发布您的配置,以便我们检查。

我发现了一个有趣的链接,它也可能对您有所帮助。这是关于 Postgres 保持自动提交模式(尽管在使用 Spring 时):

http://archives.postgresql.org/pgsql-jdbc/2007-07/msg00115.php

于 2013-01-02T19:34:48.000 回答