Hibernate插入操作未在数据库中显示,出现“JDBC事务标记为仅回滚”。

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

Hibernate INSERT not appearing in database "JDBC transaction marked for rollback-only"

问题

我正在处理一个项目的数据库部分,该项目在Java SE中使用DeltaSpike和Hibernate。

所有的SELECT语句都能正常工作,但数据库中没有任何INSERT语句。没有警告、错误或异常。

我查看了Hibernate的日志,并找到了以下内容:

[DEBUG] [05/05 00:52:16] [org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl] JDBC transaction marked for rollback-only

我尝试添加了@Transactional注解,来自javax.transactionorg.apache.deltaspike.jpa.api.transaction,以及更改了persistence.xml中的autocommit设置。

我该如何停止尝试回滚事务?

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <persistence-unit name="primary">
    <properties>
      <property name="hibernate.connection.autocommit" value="false"/>
      <property name="hibernate.hbm2ddl.auto" value="validate"/>
      <!-- 省略部分内容 -->
    </properties>
  </persistence-unit>
</persistence>

ExampleRepository.java

@Repository(forEntity = ExampleData.class)
public interface ExampleRepository extends EntityRepository<ExampleData, Long> {

}

ExampleListener.java

@Singleton
public class ExampleListener extends ExampleAdapter {

    private final ExampleRepository exampleRepo;

    @Inject
    public ExampleListener(ExampleRepository exampleRepo) {
        this.exampleRepo = Objects.requireNonNull(exampleRepo);
    }

    @Override
    public void onExample(ExampleEvent event) {
            // 省略部分内容

            ExampleData exampleData = new ExampleData(exampleId, parentId);
            exampleRepo.save(exampleData);
        }
    }
}
英文:

I'm working on the database portion of a project which uses DeltaSpike and Hibernate in Java SE.

All SELECT statements work, however no INSERT statements appear in the database. There is no warning, error, or exception.

I've gone through the logs for Hibernate and found the following:

[DEBUG] [05/05 00:52:16] [org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl] JDBC transaction marked for rollback-only

I've tried adding the @Transactional annotation from javax.transaction and org.apache.deltaspike.jpa.api.transaction, as well as changing the autocommit setting in persistence.xml.

How can I get this to stop trying to roll back the transaction?

persistence.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;persistence version=&quot;2.2&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/persistence&quot;
             xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd&quot;&gt;
  &lt;persistence-unit name=&quot;primary&quot;&gt;
    &lt;properties&gt;
      &lt;property name=&quot;hibernate.connection.autocommit&quot; value=&quot;false&quot;/&gt;
      &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;validate&quot;/&gt;
      &lt;!-- Omitted for Brevity --&gt;
    &lt;/properties&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;

ExampleRepository.java

@Repository(forEntity = ExampleData.class)
public interface ExampleRepository extends EntityRepository&lt;ExampleData, Long&gt; {

}

ExampleListener.java

@Singleton
public class ExampleListener extends ExampleAdapter {

    private final ExampleRepository emoteRepo;

    @Inject
    public ExampleListener(ExampleRepository exampleRepo) {
        this.exampleRepo = Objects.requireNonNull(exampleRepo);
    }

    @Override
    public void onExample(ExampleEvent event) {
            // Omitted for Brevity

            ExampleData exampleData = new ExampleData(exampleId, parentId);
            exampleRepo.save(exampleData);
        }
    }
}

答案1

得分: 0

问题在于在项目中错误地使用了 @Entity@Table

项目中原本是这样的,但生成的查询被称为 ExampleTable,而不是预期的 example

@Entity(name = &quot;example&quot;)
@Table
public ExampleTable {

}

解决方法是将其更改为:

@Entity
@Table(name = &quot;example&quot;)
public ExampleTable {

}
英文:

The problem was that in the project, @Entity and @Table were used incorrectly.

The project had the following, but the queries generated were called ExampleTable, not example which is what was intended.

@Entity(name = &quot;example&quot;)
@Table
public ExampleTable {

}

The solution was to change it too:

@Entity
@Table(name = &quot;example&quot;)
public ExampleTable {

}

huangapple
  • 本文由 发表于 2020年5月5日 09:14:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604201.html
匿名

发表评论

匿名网友

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

确定