简体   繁体   English

休眠:在同一会话中使用load()和get()选择相同的记录

[英]Hibernate: Selecting same record using load() and get() in the same Session

Some of the web articles are saying that 'load() method will create a proxy that engages with the unit of work related flow until the real persistent object is created with flow and later reference variable stops referring proxy and starts referring to real obj representing record' 一些Web文章说'load()方法将创建一个代理,该代理与工作单元相关的流进行交互,直到使用流创建了真正的持久对象,随后引用变量停止引用代理并开始引用代表记录的真实obj。 '

(means session will be associated with two instances to perform a load). (意味着会话将与两个实例相关联以执行加载)。

As for my understanding "load() method may return a proxy, a placeholder, without hitting the database. A consequence of this is that you may get an ObjectNotFoundException later, as soon as you try to access the returned placeholder and force its initialization". 据我了解,“ load()方法可能会返回一个代理,一个占位符,而不会触及数据库。其结果是,当您尝试访问返回的占位符并强制对其进行初始化时,您可能稍后会收到ObjectNotFoundException”。 。 (Reference-Book: Java Persistence With Hibernate). (参考书:Hibernate的Java持久性)。

It means session will be associated with only one instance that is a proxy to perform a load. 这意味着会话将一个实例关联, 该实例是执行加载的代理。

Here is an Example that shows what i have understood.. 这是一个示例,显示了我的理解。

    Session mysql_session1 = session_factory.openSession();

    logger.info("calling load()");
    POCEntity1 p = (POCEntity1) mysql_session1.load(POCEntity1.class, 12);
    /*
     * Will create a proxy which doesn't have the real data means will not
     * completely initialized until we invoke methods on it to get data.
     */
    logger.info("calling get()");

    POCEntity1 p1 = (POCEntity1) mysql_session1.get(POCEntity1.class, 12);
    /*
     * Now,the get method will directly hits the database with a select
     * query even though an object(proxy) with same identifier value already
     * associating with the session.Because the session will mark the
     * proxy's status as uninitialized ,so after calling get session will
     * re-writes the state/completes the initialization of the proxy. so p1 also
     * refers the same object.
     */

    logger.info("calling load() again ");
    POCEntity1 p2 = (POCEntity1) mysql_session1.load(POCEntity1.class, 12);

    logger.info("p==p1" + (p == p1)); //p==p1true
    logger.info("p==p2" + (p == p2)); //p==p2true
    logger.info("p1==p2" + (p1 == p2));//p1==p2true

    logger.info("p.getTitle" + p.getTitle()); 
    /*
     * will not making hibernate to generate another select query. 
     * only one select query will be generated.
     */

So,Please let me know whether i am understanding the concepts(about load(), a get() after load()) correctly or not... 所以,请让我知道我是否正确理解概念(关于load(),在load()之后是get())...

My example is saying that "get() method will uses the proxy created by the preceding load() method and completes its initialization instead of returning a new instance and referencing it. (p==p1true)". 我的示例说“ get()方法将使用由先前的load()方法创建的代理,并完成其初始化,而不是返回新实例并引用它。(p == p1true)”。

Thanks in Advance!!. 提前致谢!!。

Note: I have used assigned generator. 注意:我已经使用了分配的生成器。

You have picked it right, semantics of these methods sometimes cause confusion. 您选择正确了,这些方法的语义有时会引起混淆。 Here is practical use of two methods: 这是两种方法的实际使用:

  • You can use load in cases where you have to set a foreign key relationship, you can set a object without actually loading it. 如果必须设置外键关系,则可以使用load,而无需实际加载即可设置对象。 Load only ensures that object with that id exists and thats why you find it throwing ObjectNotFoundException 加载仅确保具有该ID的对象存在,这就是为什么您发现它ObjectNotFoundException

    Car c = new Car(); Engine e = (Stock)session.load(Engine.class, new Integer(1)); c.setEngine(e); session.save(c);

Note you only need to set engine in car you don't need actually any of the properties of engine. 请注意,您只需要在汽车中设置引擎,而实际上不需要引擎的任何属性。

  • Use get when you actually need that object. 当您实际需要该对象时,请使用get。

    Engine e = (Stock)session.get(Engine.class, new Integer(1)); System.out.println(e.getYear();

Hope this clarifies. 希望这可以澄清。

Also refer Whats the advantage of load() vs get() in Hibernate? 另请参阅Hibernate中load()与get()的优点什么?

Hibernate: Difference between session.get and session.load 休眠:session.get和session.load之间的区别

Cheers !! 干杯!

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

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