直接在数据库中更改某些东西后,Hibernate不更新实体? [英] Hibernate don't update entity after changing something directly in database?

查看:82
本文介绍了直接在数据库中更改某些东西后,Hibernate不更新实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个非常奇怪的问题,让我先显示我的配置等。这里是persitance.xml:

 < persistence-unit name =allegroTransactionPersistenceUnittransaction-type =RESOURCE_LOCAL> 
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.usernamevalue =topSecret/>
< property name =hibernate.connection.passwordvalue =topSecret/>
<! - < property name =hibernate.hbm2ddl.autovalue =create/> - >
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =show_sqlvalue =true/>
< property name =hibernate.hbm2ddl.import_filesvalue =/ SQL / payment_type.sql/>
< property name =hibernate.connection.useUnicodevalue =true/>
< property name =hibernate.connection.characterEncodingvalue =UTF-8/>
< property name =hibernate.connection.charSetvalue =UTF-8/>
< / properties>
< / persistence-unit>



用于从我的数据库中获取数据:

$ p $ @PersistenceContext(unitName =allegroTransactionPersistenceUnit,type = PersistenceContextType.EXTENDED)
受保护的EntityManager em;

公共列表< AllegroTransactionImpl> readAllegroTransactionByCreateDate()
{
TypedQuery< AllegroTransactionImpl> query = this.em.createQuery(SELECT allegroTransaction FROM com.springapp.mvc.classes.AllegroTransactionImpl allegroTransaction ORDER BY createDate DESC,AllegroTransactionImpl.class);
返回query.getResultList();
}

现在出现这个问题。如果我使用hibernate更新/删除/添加,一切都会很好。但如果我直接在基础上改变一些值(通过SQL语句)休眠不刷新它问题是为什么?

实体班



https://gist.github.com/spec8320/144100f049f54b73ad86

解决方案


  1. 请确保您已通过SQL语句提交更改。
  2. 请检查您是否已启用AllegroTransactionImpl上的二级缓存


Hi all I have pretty strange problem let me first show my configuration etc. Here is persitance.xml:

<persistence-unit name="allegroTransactionPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/AllegroShop?UseUnicode=true&amp;characterEncoding=utf8" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="topSecret" />
        <property name="hibernate.connection.password" value="topSecret" />
        <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="show_sql" value="true" />
        <property name="hibernate.hbm2ddl.import_files" value="/SQL/payment_type.sql"/>
        <property name="hibernate.connection.useUnicode" value="true" />
        <property name="hibernate.connection.characterEncoding" value="UTF-8" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
    </properties>
</persistence-unit>

And here is method that I use to take data from my database:

@PersistenceContext( unitName = "allegroTransactionPersistenceUnit", type= PersistenceContextType.EXTENDED )
protected EntityManager em;

public List<AllegroTransactionImpl> readAllegroTransactionByCreateDate()
{
    TypedQuery<AllegroTransactionImpl> query = this.em.createQuery( "SELECT allegroTransaction FROM com.springapp.mvc.classes.AllegroTransactionImpl allegroTransaction ORDER BY createDate DESC", AllegroTransactionImpl.class);
    return  query.getResultList();
}

And now the problem. Everything works great if I use hibernate to update / delete / add. but if I change some value directly in base (by SQL statement) hibernate don't refresh it question is why ?

Entity class

https://gist.github.com/spec8320/144100f049f54b73ad86

解决方案

  1. please make sure you had committed the change by SQL statement.
  2. please check if you have enabled the 2nd level cache on AllegroTransactionImpl

这篇关于直接在数据库中更改某些东西后,Hibernate不更新实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆