hibernate.connection.autocommit = true不起作用

时间:2018-09-04 16:42:57

标签: java hibernate jpa persistence.xml

我正在创建一个与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();

}

0 个答案:

没有答案