Hibernate – 确保所有实体都被持久化还是一个都不持久化?

huangapple go评论71阅读模式
英文:

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

&lt;persistence-unit name=&quot;entitystore&quot; transaction-type=&quot;JTA&quot;&gt;
    &lt;jta-data-source&gt;java:/jdbc/datasources/global&lt;/jta-data-source&gt;

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(&quot;An exception has occurred in inserting data into the table&quot; + 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 -&gt; {
    ...
    entityManager.persist(obj);
    ...
});

see : https://vladmihalcea.com/high-performance-java-persistence-github-repository/

huangapple
  • 本文由 发表于 2020年8月25日 20:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定