使用c3p0时,EntityManager.persist()和EntityManager.merge()不对数据库进行任何更改

时间:2016-02-10 14:54:15

标签: java hibernate jpa c3p0 pool

我正在使用c3p0作为连接池管理器来进行休眠。它可以很好地从数据库中检索数据,但使用JPA对数据库的任何插入或更新都不会进行任何更改。有人可以解释为什么会这样吗?

我正在使用Jboss EAP 6服务器。

的persistence.xml:

<persistence-unit name="judzi" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>com.guizion.model.Administrator</class>
    <class>com.guizion.model.BilletPayment</class>
    <class>com.guizion.model.Client</class>
    <class>com.guizion.model.Contact</class>
    <class>com.guizion.model.DepositPayment</class>
    <class>com.guizion.model.Email</class>
    <class>com.guizion.model.ExpiredEmailLink</class>
    <class>com.guizion.model.History</class>
    <class>com.guizion.model.LastSalesReportCache</class>
    <class>com.guizion.model.Location</class>
    <class>com.guizion.model.Notification</class>
    <class>com.guizion.model.Order</class>
    <class>com.guizion.model.OrderProduct</class>
    <class>com.guizion.model.Phone</class>
    <class>com.guizion.model.Product</class>
    <class>com.guizion.model.Route</class>
    <class>com.guizion.model.User</class>
    <class>com.guizion.model.Vendor</class>
    <class>com.guizion.model.VendorProduct</class>

    <properties>
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update" />

        <property name="connection.autoReconnect" value="true" />
        <property name="connection.autoReconnectForPools" value="true" />
        <property name="connection.is-connection-validation-required" value="true" />

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />

        <!-- Local -->
        <property name="hibernate.connection.username" value="****" />
        <property name="hibernate.connection.password" value="****" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/judzi" />

        <!-- C3P0 -->
        <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
        <property name="hibernate.c3p0.max_size" value="100" />
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.acquire_increment" value="1" />
        <property name="hibernate.c3p0.timeout" value="300" />
        <property name="hibernate.c3p0.max_statements" value="50" />
        <property name="hibernate.c3p0.idle_test_period" value="3000" />
    </properties>
</persistence-unit>

依赖关系(使用maven):

...
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.0.7.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.3.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>5.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
...

持久化和更新的方法:

@NoArgsConstructor
@Stateless
public class SqlRepository {

    @Inject
    private EntityManager em;

    public EntityManager getManager() {
        return em;
    }

    public <T extends BaseEntity> T add(T item) {
        if (item.hasValidId())
            return update(item);
        this.em.persist(item);
        this.em.flush();
        return this.refresh(item);
    }

    public <T extends BaseEntity> T update(T item) {
        return this.em.merge(item);
    }

    ...
}

0 个答案:

没有答案