users@glassfish.java.net

Re: Lazy/eager fetch question

From: Kenneth Clark <kenneth_at_rabiddog.co.za>
Date: Thu, 08 Feb 2007 14:13:28 +0200

I had a similiar problem with the JPA. I found that if I initialized the
object inside the default constructor I could do away with the EAGER
fetch. I am working on a seperate theory at the moment and once I deploy
it I will let you know.

Kenneth Clark
Developer / Analyst

Rabid Dog Laboratories ™
Putting the art back into development

*tel:* +27 11 475 7409
*mobile:* +27 82 500 5090
*e-mail:* kenneth_at_rabiddog.co.za
*website:* http://www.rabiddog.co.za/


glassfish_at_javadesktop.org wrote:
> Hello
>
> I have three entities:
> User, UserBatch, UserAttribute
>
> In my User entity I have:
> @OneToMany(cascade = CascadeType.ALL, mappedBy = "userid")
> private Set<Userbatch> userbatchCollection;
>
> @OneToMany(cascade = CascadeType.ALL, mappedBy = "userid")
> private Set<Userattribute> userattributeCollection;
>
> In UserBatch and UserAttribute entities i have:
> @JoinColumn(name = "USERID", referencedColumnName = "USERID")
> @ManyToOne
> private Userprofile userid;
>
> In Session bean, I have the following method to find a User
> Now in my client, if i do the following:
> User up = mysession.findUser(Long.valueOf(9));
> System.out.println(up.getUserattributeCollection().size());
>
> I get the following exception:
> org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entities.User.userattributeCollection, no session or session was closed
> at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
> at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
> at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
> at org.hibernate.collection.PersistentSet.size(PersistentSet.java:114)
>
> If I make the following changes everything works fine:
>
> @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "userid")
> private Set<Userattribute> userattributeCollection;
>
> @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "userid")
> private Set<Userbatch> userbatchCollection;
>
> But i do not want to use EAGER since the User table has a lot of data and everytime there is a query for User, all the related collections will be Eagerly fetched.
>
> What should I do?
>
> Thanks
> [Message sent by forum member 'rabbiaqaswar' (rabbiaqaswar)]
>
> http://forums.java.net/jive/thread.jspa?messageID=202638
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>
>
>
>