带有Hibernate的Spring Data JPA - OnetoMany关系映射实体 - 部分提交问题

时间:2016-01-17 04:13:29

标签: java spring hibernate jpa spring-transactions

我在两个实体类之间有一对多的关系。在插入子项期间发生错误时,会发生父项的部分插入,但子记录仍然是未持久的。这里配置的EntityManager应该处理事务的回滚,但事实并非如此。

这是我的实体类:

@Entity
@Table(name="parent")
public class Parent implements Serializable { 

   @Id
    @SequenceGenerator(name = "seq_parent", sequenceName = "seq_parent")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_parent")
    @Column(name="PARENT_ID")
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy="parent")
    private Set<Child> childDetails;

}


@Entity
@Table(name="child")
public class Child implements Serializable {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PARENT_ID")
    private Parent parent;

}

Now my Service Class Method :

public class ServiceImpl implements Service {

 @PersistanceContext
 private EntityManager entityManger;

 @Transactional
 public void persist() {
    Parent parent = new Parent();
    Set<Child> childSet = new HashSet<>();
    Child child = new Child();
    child.setParent(parent);
    childSet.add(child );
    parent.add(childSet);
    entityManager.persist(parent);        
 }
}

Here are my JavaConfig in Spring for transactionManager and DataSource :



@Configuration
@ImportResource({ "classpath:META-INF/spring/audit/auditing-dbsink-config.xml",
        "classpath:META-INF/spring/services-context.xml" })
@EnableTransactionManagement(order = 20)
public class DatabaseConfiguration implements EnvironmentAware {

}

app-datasource.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


<tx:jta-transaction-manager/>
    <context:annotation-config/>

    <jee:jndi-lookup id="datasource" jndi-name="java:jboss/datasources/jdbc/datasource" lookup-on-startup="true"
                     proxy-interface="javax.sql.DataSource"/>

   <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          parent="abstractEntityManagerFactory" lazy-init="true">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="oracle_pu"/>
    </bean>

</beans>

现在问题是,如果在保留子记录时发生异常,则TransactionManager不会回滚整个事务。

请注意这点,因为我真的无法继续前进。

1 个答案:

答案 0 :(得分:1)

默认情况下,抛出已检查的异常时,Spring不会回滚事务。来自documentation

  

在其默认配置中,Spring Framework的事务   基础结构代码 only 标记了回滚中的事务   运行时的情况,未经检查的异常;就是扔的时候   exception是RuntimeException的实例或子类。 (错误   也将 - 默认情况下 - 导致回滚)。检查异常   从事务方法抛出的 not 结果   在默认配置中回滚。

有关如何更改此行为的更多详细信息,请参阅链接文档。例如,我发现最直观的方法是回滚所有异常的事务:

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="*" rollback-for="Throwable" />
    </tx:attributes>
</tx:advice>