英文:
Hibernate - ensure that all entities are persisted or none?
问题
我有以下代码将两个不同的“entities”持久化到我的“MYSQL”数据库中。
这按预期工作,但是如果一个表格出现问题而另一个表格没有问题,那么一个表格会被填充,而另一个表格不会被填充。
注意 - 我将我的应用程序作为“EAR”文件运行在“jboss EAP”服务器中。
我希望确保要么两个表都被填充,要么都不填充。
如何做到这一点?
Persistence.xml
<persistence-unit name="entitystore" transaction-type="JTA">
<jta-data-source>java:/jdbc/datasources/global</jta-data-source>
Java service class:
public void createCompanyStatuses(String client, CompanyStatusPostDTO companyStatusPostDTO) {
EntityManager entityManager = null;
try {
CompanyStatus companyStatus = new CompanyStatus();
companyStatus.setCompanyLabel(candidateMaskedStatusPostDTO.getCompanyLabel());
entityManager = entityManagement.createEntityManager(client);
entityManager.persist(companyStatus);
for(Integer employeeStatusId: companyStatusPostDTO.getEmployeeStatuses()){
CompanyStatusEmployeeStatus companyStatusEmployeeStatus = new CompanyStatusEmployeeStatus();
companyStatusEmployeeStatus.setEmployeeId(employeeStatusId);
companyStatusEmployeeStatus.setCompanyId(companyStatus.getCompanyId()); //todo - how will get this?
entityManager.persist(CompanyStatusEmployeeStatus);
}
} catch(Exception e){
log.error("An exception has occurred in inserting data into the table" + e.getMessage(), e);
} finally {
entityManagement.closeEntityManager(client, entityManager);
}
}
Edit:
我已尝试添加:
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
然而,问题仍然存在,成功的持久化工作,而不成功的不工作 - 而不是全部或无事发生。
英文:
I have the following code to persist two different entities
to my MYSQL
DB.
This works as expected, however if there is an issue with one table and not the other then one table is getting populated, and the other not.
Note - I am running my application as an EAR
file within a jboss EAP
server.
I want to ensure that either both tables are populated or none.
How can I do so?
Persistence.xml
<persistence-unit name="entitystore" transaction-type="JTA">
<jta-data-source>java:/jdbc/datasources/global</jta-data-source>
Java service class:
public void createCompanyStatuses(String client, CompanyStatusPostDTO companyStatusPostDTO) {
EntityManager entityManager = null;
try {
CompanyStatus companyStatus = new CompanyStatus();
companyStatus.setCompanyLabel(candidateMaskedStatusPostDTO.getCompanyLabel());
entityManager = entityManagement.createEntityManager(client);
entityManager.persist(companyStatus);
for(Integer employeeStatusId: companyStatusPostDTO.getEmployeeStatuses()){
CompanyStatusEmployeeStatus companyStatusEmployeeStatus = new CompanyStatusEmployeeStatus();
companyStatusEmployeeStatus.setEmployeeId(employeeStatusId);
companyStatusEmployeeStatus.setCompanyId(companyStatus.getCompanyId()); //todo - how will get this?
entityManager.persist(CompanyStatusEmployeeStatus);
}
} catch(Exception e){
log.error("An exception has occurred in inserting data into the table" + e.getMessage(), e);
} finally {
entityManagement.closeEntityManager(client, entityManager);
}
}
Edit:
I have tried adding:
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
However, the issue still remains that the successful persists work and the non-successful don't - rather that all or nothing being persisted.
答案1
得分: 1
只需使用事务。
在Spring中,使用@Transactional注解。
如果没有Spring框架,您可以这样做:
doInJPA(entityManager -> {
...
entityManager.persist(obj);
...
});
详情请参阅:https://vladmihalcea.com/high-performance-java-persistence-github-repository/
英文:
Simply use a transaction.
With spring use the @Transactional annotation.
Without spring framework, you can do
doInJPA(entityManager -> {
...
entityManager.persist(obj);
...
});
see : https://vladmihalcea.com/high-performance-java-persistence-github-repository/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论