• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

many-to-one data retrieval on the ONE side

 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i have a relation many-to-one, when i do retrieval, how can i get the data from the "one" side? i have the get and set method for this many-to-one relationship(oneInfo)

i tried this way... but it gives me error like "session closed..."

getOneInfo().getFieldNameFromOneSide()

kindly advise. Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jolie Lee:

i tried this way... but it gives me error like "session closed..."

getOneInfo().getFieldNameFromOneSide()


Well, is the session still open? Remember if you want to get an association which has not already been populated Hibernate will try to lazily initialize this association. If the session has been closed then you can't perform any Database operations. Could you show more of how you use this POJO?
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i do



dList is a list where i keep all the data i retrieved from listDByCriteria. the getBgId is the id from the "one" side of the relationship.

and the error when i run is

09:36:30,006 ERROR [LazyInitializationException] could not initialize proxy - the owning Session was closed

[ August 31, 2005: Message edited by: Jolie Lee ]
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, this is strange, if i do a system.out.print before i do tx.commit(), then i can get the value from the "one" side without any error, but without the system.out.println, i'll get the session close error..

anybody can explained to me why?
 
Vibha Jindal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works with teh system.out before commit, as it explicitly loads the object (BgId) when u call its get method in the system.out

To fix this, you need to do either of the following:
1. Make lazy=false in the xml mapping of the "one" side class where it refers the set of the many side.
This will ensure that teh many side is ALWAYS loaded whenever the one side is loaded.
Or
2.If you don't want to load the many side everytime you load the one side, you can explicitly call Hibernate.initialize(oneSideObject.getManySideObjects()); wherever you DO want to load it.

MAke sure you don't call oneSideObject.getManySideObjects() once the session is closed , if either of teh above have not been done before the session is closed.
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried putting lazy=false at the one side, but i still got that error..

hmm.. wat i was trying to do was getting the results from the one side out of the many side... does that explain why the lazy=false doesn't works for me?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic