Hibernate在会话中返回值,但不是从数据库中返回值 [英] Hibernate returning the values in session, but not from the database

查看:119
本文介绍了Hibernate在会话中返回值,但不是从数据库中返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

  getHibernateTemplate()。get(A.class,100)

从数据库中删除。假设这个实体'A'在数据库中有一个属性'value'200.



现在,在我的Java代码中,我更改了这个实体的属性。可以说,我将'value'属性更改为'500',然后将它添加到列表中。



现在,如果我再次执行getHibernateTemplate()。get(A .class,100)为同一个实体,我得到更新的实体(值为500)。如何强迫hibernate从数据库中获取实体,但不是我的代码中更新的实体?



这是所谓的一级缓存吗?

解决方案

您的假设(关于第一级缓存)是正确的。例如,在此处声明:界面会话


Java应用程序和Hibernate之间的主要运行时接口。
这是提取持久性服务概念的中心API类。


或者这里。 2.1。概览



提取:会话(org.hibernate.Session)


<一个单线程,短暂的对象,表示应用程序和持久性存储之间的对话。它封装了JDBC
连接,并且是Transaction的工厂。 Session持有一个强制性
的一级缓存,用于在浏览
对象图或通过标识符查找对象时使用的持久对象。


另外,您可以看到我们可用的方法,从会话中删除一个对象:


  • evict(Object object):
    从会话缓存中删除此实例。


  • refresh(Object object):
    Re-


  • clear():
    完全清除会话。

  • ul>

    等等。 Evict 在这种情况下应该是有效的。我们必须从会话中获取当前实例('A')并显式 Evict >。

    如果我们已经加载了一些/更多的东西,而且我们不知道, Evict() ,我们只需要获取最新的数据。然后我们可以调用 Clear()来完全重置会话并重新开始。

    这有点激进,因为会话中没有任何对象会被更新/插入会话 Flush() ...但它可能是我们在这种情况下想要的(通常用于测试...加载,清除...更改和刷新)

    I get an entity 'A' using

    getHibernateTemplate().get(A.class, 100) 
    

    from the database. Lets say this entity 'A' has a property 'value' 200 in the database.

    Now, in my Java code, I change a property for this entity. lets say, I change the 'value' property to '500' and then add it to some list.

    Now, If I again do getHibernateTemplate().get(A.class, 100) for the same Entity, I am getting the updated entity(that has a value of 500). How do I force hibernate to get me the entity from the database, but not the one updated in my code?

    Is this what is called as 'First Level Caching'?

    解决方案

    Your assumption (about first level caching) is correct. As for example stated here: Interface Session:

    The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.

    Or here Chapter 2. Architecture; 2.1. Overview

    Extract: Session (org.hibernate.Session)

    A single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC connection and is a factory for Transaction. Session holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.

    And also, you can see the methods available for us, to remove an object form the session:

    • evict(Object object): Remove this instance from the session cache.

    • refresh(Object object): Re-read the state of the given instance from the underlying database.

    • clear(): Completely clear the session.

    And many more. Evict in this case should be working. We have to take the current instance ('A') and explicitly Evict it from the session.

    If we've already loaded some/more stuff, and we do not know, what to Evict(), we simply need to get the fresh data. Then we can call Clear() to completely reset the session and start again.

    This is a bit radical, because none of the objects in the session will be updated/inserted on session Flush()... but it could be what we want in this scenario (very often used for testing... load, clear... change and flush)

    这篇关于Hibernate在会话中返回值,但不是从数据库中返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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