Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding load

 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code should have thrown the
org.hibernate.LazyInitializationException:
could not initialize proxy - no Session


As i m trying to using the load method and trying to access the session object outside the transactional context. But its working fine for me , no exception at all and i m succesfully able to return the results. Anyone can throw some light on it , why is it behaving like this.
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
Please reply
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a session, so no error with "no session" can appear... You just commited the transaction so Hibernate starts a new one when you execute the next statement.
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After commiting one transaction , i did not start any new transaction
does hibernate start trnsaction automatically?

 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check session.getTransaction().isActive() before and after calling the get-Method.
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i checked the same before and after the getter method.

Both calls to isActive returned false.
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from Java Persistence with Hibernate:
"More important, the load() method may return a proxy, a placeholder, without hitting the database. ... The load() method always tries to return a proxy, and only returns an initialized object instance if it's already managed by the currrent persistence context."

Do you see a select-statement when you call the get-method?
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , i do see the select statement when getter gets called...Important point over here is session is under no transactional context, how can it result in firing the query after commiting the transaction


Transaction started
BEFOREfalse
Hibernate: select user0_.id as id1_0_, user0_.password as password1_0_ from User user0_ where user0_.id=?
transaction after commitingabc123
AFTERfalse


 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.beginTransaction();
System.out.println("Transaction started");
User user = (User)session.load(User.class, new Long(1));
session.getTransaction().commit();


Are you sure, you are committing the correct transaction? The beginTransaction, returns the transaction instance which was created:

 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i believe getTransaction should return the same instance of transaction which it creates when calling beginTransaction function.

However just to make sure i tried with the code scnippet the one you wrote . but still result is the same
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,I m still having the above doubt in behaviour of load.
Anyone , please answer?
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to close the session and than call the u.getPassword
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i close session after the call to commit. that throws the

org.hibernate.LazyInitializationException


question is how can we have a valid session outside the transactional context?
And this is What i read in the "Hibernate Made Easy" book (page 121) .
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys...i m stil having someone to clarify this doubt?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic