• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

understanding load

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
Please reply
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,I m still having the above doubt in behaviour of load.
Anyone , please answer?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guys...i m stil having someone to clarify this doubt?
reply
    Bookmark Topic Watch Topic
  • New Topic