直接在数据库中更改某些东西后,Hibernate不更新实体? [英] Hibernate don't update entity after changing something directly in database?
问题描述
大家好我有一个非常奇怪的问题,让我先显示我的配置等。这里是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
- 请确保您已通过SQL语句提交更改。
- 请检查您是否已启用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&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
- please make sure you had committed the change by SQL statement.
- please check if you have enabled the 2nd level cache on AllegroTransactionImpl
这篇关于直接在数据库中更改某些东西后,Hibernate不更新实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!