繁体   English   中英

从数据库JPA正确刷新数据-Hibernate

[英]Properly Refresh data from Database JPA - Hibernate

我目前正在使用JPA规范从数据库查询我的对象。

每当我(我的软件实例)进行更改时,项目都会被正确刷新。

但是,如果其他人进行了其他更改(其他实例或数据库更改),则不会正确刷新项目。

我正在使用简单的“查找”和“刷新”

Object found = getManager().find(getModelClass(), id);
getManager().refresh(found);

我正在使用DAO层次结构,“ getModelClass”返回我的@Entity类,例如

@Override
protected Class<?> getModelClass() {
    return ProductCategory.class;
}

而我的清单/ persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="casa" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

            <!-- localhost -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />


            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />
            <property name="hibernate.jdbc.wrap_result_sets" value="false" />
            <property name="hibernate.hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" /> 

        </properties>

    </persistence-unit>
</persistence>

另外,每个“ DAO”都有其自己的实例和EntityManager。

我可能做错了什么?

感谢帮助!

看来我的问题很简单。

需要提交要从数据库正确刷新的刷新事务

getManager().getTransaction().begin();
T found = (T) getManager().find(getModelClass(), id);
getManager().refresh(found);
getManager().getTransaction().commit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM