-->
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.  [ 2 posts ] 
Author Message
 Post subject: LazyInitializationException Session Bean EJB 3.0
PostPosted: Fri Feb 05, 2010 8:06 pm 
Newbie

Joined: Fri Feb 05, 2010 7:46 pm
Posts: 1
Hi guys!

I'm trying to develop a very basic Stateless Session Bean, using JPA (Hibernate) and trying to run in JBoss 5.1.0 GA. Here's the thing. I have these entity beans User.java and UserCase.java. I'm need a one-to-many unidirectional relationship, so here's the code:

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

   private int userId;
   private String firstName;
   private String lastName;
   private String login;
   private String password;

   private List<UserCase> userCases;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public int getUserId() {
      return userId;
   }

   public void setUserId(int userId) {
      this.userId = userId;
   }

        /** Getters and Setters */

   @OneToMany(cascade = { CascadeType.ALL })
   @JoinTable(name = "user_user_case", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "userCaseId") })
   public List<UserCase> getUserCases() {
      return userCases;
   }

   public void setUserCases(List<UserCase> userCases) {
      this.userCases = userCases;
   }

}


And UserCase.java

Code:
@Entity
@Table(name="user_case")
public class UserCase implements Serializable{

   private int userCaseId;
   private String status;
   private Date createdDate;
   private Date finishDate;
   private int state;
   private int currentIteration;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public int getUserCaseId() {
      return userCaseId;
   }

   public void setUserCaseId(int userCaseId) {
      this.userCaseId = userCaseId;
   }

   @Temporal(TemporalType.TIMESTAMP)
   public Date getCreatedDate() {
      return createdDate;
   }

   public void setCreatedDate(Date createdDate) {
      this.createdDate = createdDate;
   }

        /** Getters and Setters */

}


The session bean just try to get the collection in User.java. This is the interface:

Code:
@Remote
public interface UserRemote {

   List<UserCase> getUserCases(User user);
}


And this is the implementation Bean:

Code:
@Stateless
public class UserBean implements UserRemote {

   @PersistenceContext(unitName = "test")
   EntityManager manager;

   public List<UserCase> getUserCases(User user) {

      User userDb = manager.find(User.class, user.getUserId());
      return userDb.getUserCases();
   }

}


I develop a client, very basic ... to test my session bean:

Code:
public class SimpleSessionClient {
   public static void main(String[] args) throws Exception {
      InitialContext ctx = new InitialContext();
      UserRemote userRemote = (UserRemote) ctx.lookup("UserBean/remote");

      User user = new User();
      user.setLogin("caroll");
      user.setPassword("caroll");
      user.setUserId(1);

      List<UserCase> userCases = userRemote.getUserCases(user);
      
      System.out.println("userCases: " + userCases);

   }
}


As you can see the method in the session bean is performing a find using the user's id. That's all!!!! There's one more thing, I know that hibernate manage the collections LAZYLY by default. This could be perfectly solved if I put fetch=EAGER, but I can't do that! I also know that if I call the size() method in the List I solve my problem, but it seems like a bad solution! I should be another way to do it! How can I get the List of UserCase????????

This is the Exception I'm getting when I try to run my client:

Code:
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: test.model.User.userCases, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
   at org.hibernate.collection.PersistentBag.toString(PersistentBag.java:506)
   at java.lang.String.valueOf(Unknown Source)
   at java.lang.StringBuilder.append(Unknown Source)
   at client.SimpleSessionClient.main(SimpleSessionClient.java:23)


Thanks very much for your help! I'm really DESPERATE cause this seems to easy but It's not working at all!


Top
 Profile  
 
 Post subject: Re: LazyInitializationException Session Bean EJB 3.0
PostPosted: Sun Feb 07, 2010 7:37 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You're calling the getter after the session is closed. If you could just put a little getter call into your code before the session is closed, all would be well!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.