-->
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.  [ 6 posts ] 
Author Message
 Post subject: FetchType.EAGER - what's wrong with my annotations?
PostPosted: Mon Jan 30, 2006 1:18 pm 
Newbie

Joined: Thu Jul 28, 2005 5:53 am
Posts: 9
Location: Poland
Hi,

I have a simple relationship that is configured(AFAIK) to use eager loading of associated collection - I want to load the data eagerly to be able to use getters after a session is closed.

Code:
@Entity()
@Table(name = "user")
public class User {

   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   public Set<Place> getPlaces() {
      return places;
   }
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "placetype", discriminatorType = DiscriminatorType.STRING)
@Table(name = "place")
public abstract class Place {

   @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
   @JoinColumn(nullable = false)
   public User getUser() {
      return user;
   }
}



However when a user is loaded it is of a proxy type and the places set in not initialized. I get a well known "org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed" when invoking a getPlaces method.

Could you give me any hint of how to properly configure eager loading. (As you can see in the code fetch = FetchType.EAGER is set - but is it correctly set?)


Thanks,

Maciek


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I find it hard to believe. There are unit tests for EAGER fetching in Hibernate Annotations, have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:53 am 
Newbie

Joined: Thu Jul 28, 2005 5:53 am
Posts: 9
Location: Poland
To give you more details:

The exception occurs when a getter is invoked when a session is already closed. I don't know Hibernate well and don't know if it is correct, but a user object returned is of proxy type. Afterwards when a getter is invoked a CGLIBLazyInitializer.intercept and AbstractLazyInitializer is invoked which throws the exception.

When I added @Proxy(lazy=false) in both a User and a Place class the eager loading works.

Code:
@Entity()
@Table(name = "user")
@Proxy(lazy = false)
public class User {..}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "placetype", discriminatorType = DiscriminatorType.STRING)
@Table(name = "place")
@Proxy(lazy = false)
public abstract class Place {}


Where do you suggest to take a look at in the debuggger to find what;s wrong?

Thanks,

Maciek


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 12:42 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 10:34 am
Posts: 38
I don't think, the association to Places is your problem.
As you wrote, the instance of User is already a proxy.

I'd guess you are using Session.load() to retrieve the User.
The method load() always returns a proxy - even if the is no persistent instance with the given identifier. Use Session.get() instead.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 12:48 pm 
Newbie

Joined: Thu Jul 28, 2005 5:53 am
Posts: 9
Location: Poland
Yes, you're right. I used sesion.load. After switching to session.get it started working :)

Thanks,

Maciek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 4:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
so please rate him

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.