英文:
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.transaction
和org.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
<?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"/>
<!-- Omitted for Brevity -->
</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 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 = "example")
@Table
public ExampleTable {
}
解决方法是将其更改为:
@Entity
@Table(name = "example")
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 = "example")
@Table
public ExampleTable {
}
The solution was to change it too:
@Entity
@Table(name = "example")
public ExampleTable {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论