java - hibernate.connection.autocommit = true 不工作
问题描述
我正在创建一个连接到 Oracle XE 的新 java 项目...
我persistence.xml
用这个配置了我的:<property name="hibernate.connection.autocommit" value="true" />
但是在我的 DAO 上,如果我不使用em.getTransaction().begin();
并且em.getTransaction().commit();
我的实体没有持久化......
我如何才能使用em.persist(entity);
?
更新:
我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
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_1.xsd">
<persistence-unit name="simpleRestApplication" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- Configuração do driver -->
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<!-- Configuração de conexão -->
<property name="hibernate.connection.url"
value="jdbc:oracle:thin:@localhost:1521/XE" />
<property name="hibernate.connection.username"
value="system" />
<property name="hibernate.connection.password"
value="********" />
<property name="hibernate.connection.autocommit"
value="true" />
<!-- Configuração do hibernate -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.release_mode"
value="auto" />
<property name="current_session_context_class"
value="thread" />
<property name="hibernate.connection.autoReconnect"
value="true" />
</properties>
</persistence-unit>
</persistence>
在我的 DAO 上,我用 GoogleGuice 注入 entityManager ......
@Inject
private EntityManager em;
我的 GuiceModule 是这个...
@Singleton
private static class JPAInitializer {
@Inject
public JPAInitializer(final PersistService service) {
service.start();
}
}
@Override
protected void configure() {
install(new JpaPersistModule("simpleRestApplication"));
bind(JPAInitializer.class).asEagerSingleton();
}