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!