两个实体类之间存在一对多关系。当插入子记录期间发生错误时,会发生部分插入父记录,但子记录仍不保留。这里配置的 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 不会回滚事务。来自 documentation :
In its default configuration, the Spring Framework’s transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. ( Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.
有关如何更改此行为的更多详细信息,请参阅链接文档。例如,我发现最直观的方法是回滚所有异常的事务:
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" />
</tx:attributes>
</tx:advice>
关于java - Spring Data JPA 与 Hibernate - 一对多关系映射实体 - 部分提交问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835018/