繁体   English   中英

具有Hibernate的Spring Data JPA-一对多关系映射实体-部分提交问题

[英]Spring Data JPA with Hibernate - OnetoMany Relationship Mapped Entities - Partial Commit Issue

我在两个实体类之间有一对多的关系。 如果在插入子项期间发生错误,则发生父项的部分插入,但子记录保持不变。 这里配置的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不会回滚整个事务。

请对此加以说明,因为我真的无法继续进行下去。

默认情况下,抛出检查异常时,Spring不会回滚事务。 文档中

在默认配置中,Spring框架的事务基础结构代码在运行时,未经检查的异常的情况下将事务标记为回滚。 也就是说,当抛出的异常是RuntimeException的实例或子类时。 (默认情况下,错误也会导致回滚)。 从事务方法引发的检查异常不会导致默认配置中的回滚。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM