-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Simple O/R Mapping question.
PostPosted: Mon Jan 15, 2007 10:48 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
I have a Foo object that havs an List with one or more Bars objects. When Im trying to run Foo.getBars() Im getting LazyInitializationException. Here is my settings.



Hibernate version:
jboss-4.0.5.GA (is or is based on Hibernate 3, right?)

Mapping documents:
Foo:
Code:
@OneToMany(mappedBy="foo",targetEntity=Bar.class)
public List<Bar> getBars() {...}


Bar:
Code:
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(name="foo_id",referencedColumnName="id")
public Foo getFoo() {...}


Im getting (on runtime) Error:
Code:
LazyInitializationException: failed to lazily initialize a collection of role: com.foobar.fooList, no session or session was closed.



Here is how I get my EntityManager in Handlers:
Code:
@PersistenceContext(unitName="FooBarContext")
private EntityManager em;


However I have seprate Handelers for Foo & Bar. Its that ok? Or should I make one Handeler for every Entity in my Application.

I know whats the problem, when Im retreving my Foo its dosent load all the Bars. This can be "overried" by FetchType.EAGAR. However it dosent work in my case, Im getting NullPointer on my Bar.Hashcode(). And would like to solve it "lazy" way first.


Name and version of the database you are using:
Mysql 4.0.18

Thanks for your time!


Top
 Profile  
 
 Post subject: More info needed
PostPosted: Mon Jan 15, 2007 5:57 pm 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
Post your hashCode method and other mapped fields.
Also, where are you accessing the (lazy) collection of Bars?

The session is probably closed (as the error message says).

You should be able to access the list of bars within the same transaction (should certainly work, I'm assuming you access the lazy collection somewhere in a JSP after the session is closed).

You can initialize using hql:
Code:
select Foo f left join fetch f.bars

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 4:51 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Your are assuming right. My jsp look like this.

jsp:
Code:
Foo foo = service.getFooById(1);
foo.getBars(); // Rises Exception.


Here is my hashcode for bar:
Code:
   public int hashCode() {
   return 27 * this.getId().hashCode();
   }


I Think my mapping settings are right due no error when Im deploying the jar-file.

Could you please give an more concrete example where and how to initialize the HQL-query. Should the initialize be in the entity using the @NamedQuery Annotation?

Cheers!


Top
 Profile  
 
 Post subject: Where is your query?
PostPosted: Tue Jan 16, 2007 4:56 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
Somewhere you have an HQL query to read the list of 'Foo' object, right? You can add the initialization in that query (with the 'left join fetch' syntax)

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject: Oops, replied too fast
PostPosted: Tue Jan 16, 2007 5:01 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
In your service class :-)

If you use find(), you could change that to use a query instead (and add the fetch part).
Or force the initialization by accessing the collection in the service before returning it to the JSP.

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 5:39 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Thanks for the fast replies. I manged to solve the EAGER-nullpointer by this thread. http://forum.hibernate.org/viewtopic.php?t=960270&highlight=&sid=bd9ae93f9ad9e42e5b3b3a54864b4241

I still would like to solve it the lazy way, I will get back and hand out credits if it turns out if you guys are right ;).

Quote:
Somewhere you have an HQL query to read the list of 'Foo' object, right?

Yes in my DAO:
Code:
   public Foo getFootById(Long id) {
      Query dbQuery = em.createQuery("from Foo where id = :id");
      dbQuery.setParameter("id", id);
      return (Foo) dbQuery.getSingleResult();
   }


And you mean?

Code:
   public Foo getFootById(Long id) {
      Query dbQuery = em.createQuery("from Foo f left join f.bars where f.id = :id");
      dbQuery.setParameter("id", id);
      return (Foo) dbQuery.getSingleResult();
   }


And f.bars should be my List of bars in my entity Foo, right or wrong? Dont se how to use the find()...

Cheers!


Top
 Profile  
 
 Post subject: try
PostPosted: Tue Jan 16, 2007 5:42 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
Also add 'fetch' in the query, like this:
Code:
from Foo f left join fetch f.bars where f.id=:id


Then it should work.

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 6:56 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
I cant get lazy to work, and I havent got more time to this problem. I think i have to change the:

Code:
return (Foo) dbQuery.getSingleResult();


Because the join returns mores then one row in the ResultSet? But even if did get this to work. Is it the right way to force loading list? Feels litte like Im going away from the Hibernatevision?


Top
 Profile  
 
 Post subject: You can use a filter
PostPosted: Tue Jan 16, 2007 7:01 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
The hibernate-vision can be to use a session per request.
You open the session (and start a transaction) in a servlet-filter and commit at the end. Lazy loading then works (see the wiki for this)
But when using session beans, this can get more complicated (transaction handling is normally done in the ejb-layer, not in the web-layer).

SEAM handles all this very nice, but you will have to start using JSF then as well.

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 7:43 am 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Ok, thanks for the tip. Maybe next project.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.