[jboss-user] [JBoss Seam] - PersistenceContext in distributed environment

hamtho2 do-not-reply at jboss.com
Mon Jan 22 10:48:59 EST 2007


I want to split up my seam application into 2 different application, so that I can use one JBoss as a webserver and another one as the application-server with the underlyng database. Therefor I use the EntityManager only on the application-server, so that all the updates etc. on the database all go through the same method. Now I experience problems when using my entities and want to use object, that are lazy-loaded. 

One example, I have a User-object with a UserLog:

  | @Name("user")
  | @Scope(ScopeType.SESSION)
  | public class User implements Serializable {
  | ...
  | @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch=FetchType.LAZY)
  |     private Set<UserLog> userLogCollection ;
  | ...
  |     public Set<UserLog> getUserLogCollection() {
  |         return this.userLogCollection;
  |     }
  |     protected void setUserLogCollection(Set<UserLog> userLogCollection) {
  |         this.userLogCollection = userLogCollection;
  |     }
  |     public void addUserLog(UserLog userLog) {
  |         if (this.userLogCollection == null) {
  |             this.userLogCollection = new HashSet();
  |         }
  |         this.userLogCollection.add(userLog);
  |     }

When I Login I query the EnitityManager through my application-server and return the user-object.
When I try to add a new UserLog-Object using the methods of my entity object I get a LIE, probably because the user-object is detached:

  | 16:45:59,978 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: common.entity.User.userLogCollection, no session or session was closed
  | org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: common.entity.User.userLogCollection, 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.initialize(AbstractPersistentCollection.java:343)
  |         at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
  |         at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
  |         at java.lang.String.valueOf(String.java:2615)
  |         at java.lang.StringBuilder.append(StringBuilder.java:116)

When I do this through a method on my application-server together with a em.merge, everything works fine, because the entity becomes managed again. But this cannot be the solution, as would have to write a method for every lazy-loaded object on my application-server.

  | public User addUserLog(User user, UserLog userLog) {
  |         User userTmp = (User) em.merge(user);
  |         userTmp.addUserLog(userLog);
  |         return userTmp;
  |     }

I tryied to work with the Seam Managed Persistence Context, but as far as I could see in the documentation I have to initialize the EntityManager through the Seam- at In-Annotation, which doesn´t seem very good, if I would like to have seam-independent controller-beans on my application server.

What would be the best solution to work with lazily-loaded objects in a distributed environment? Does anyone have some experiences with that and therefor has a good recommendation?

Thank you very much


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4004859#4004859

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4004859

More information about the jboss-user mailing list