我正在创建一个与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上,我向EntityManager注入了GoogleGuice ...
@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();
}