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'
(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". (Reference-Book: Java Persistence With Hibernate).
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...
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)".
Thanks in Advance!!.
Note: I have used assigned generator.