[jboss-user] [JBoss Seam] - Re: EntityManger-per-user-session...

bkyrlach do-not-reply at jboss.com
Wed Nov 8 10:25:11 EST 2006


Okay, that makes a lot more sense to me. However, things still don't seem to be working quite the way you describe. Here's my newly reworked code, using two generic beans, Parent and Child.

Parent bean...


  | package sample.model.pc;
  | 
  | import java.util.List;
  | 
  | import javax.persistence.CascadeType;
  | import javax.persistence.Entity;
  | import javax.persistence.FetchType;
  | import javax.persistence.GeneratedValue;
  | import javax.persistence.Id;
  | import javax.persistence.OneToMany;
  | import javax.persistence.Version;
  | 
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Role;
  | import org.jboss.seam.annotations.Scope;
  | 
  | @Entity
  | @Name("parent")
  | @Scope(ScopeType.SESSION)
  | @Role(name="newParent", scope=ScopeType.EVENT)
  | public class Parent
  | {
  |     Long id;
  |     Long version;
  |     String name;
  |     List<Child> children;
  |     
  |     public Parent(Long id, Long version, String name, List<Child> children)
  |     {
  |         // TODO Auto-generated constructor stub
  |         this.id = id;
  |         this.version = version;
  |         this.name = name;
  |         this.children = children;
  |     }
  | 
  |     public Parent() {}
  |         
  |     @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="parent")
  |     public List<Child> getChildren()
  |     {
  |         return children;
  |     }
  |     public void setChildren(List<Child> children)
  |     {
  |         this.children = children;
  |     }
  | 
  |     @Id @GeneratedValue
  |     public Long getId()
  |     {
  |         return id;
  |     }
  |     public void setId(Long id)
  |     {
  |         this.id = id;
  |     }
  | 
  |     public String getName()
  |     {
  |         return name;
  |     }
  |     public void setName(String name)
  |     {
  |         this.name = name;
  |     }
  | 
  |     @Version
  |     public Long getVersion()
  |     {
  |         return version;
  |     }
  |     public void setVersion(Long version)
  |     {
  |         this.version = version;
  |     }
  | 
  |     public boolean equals(Object o)
  |     {
  |         if(o instanceof Parent)
  |         {
  |             return o.toString().equals(this.toString());
  |         }
  |         return false;
  |     }
  |     
  |     public String toString()
  |     {
  |         return this.getClass() + "@" + name;
  |     }
  |     
  |     public int hashCode()
  |     {
  |         return this.toString().hashCode();
  |     }
  | }
  | 

Child bean...


  | package sample.model.pc;
  | 
  | import javax.persistence.Entity;
  | import javax.persistence.GeneratedValue;
  | import javax.persistence.Id;
  | import javax.persistence.ManyToOne;
  | import javax.persistence.Version;
  | 
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Scope;
  | 
  | @Entity
  | @Name("child")
  | @Scope(ScopeType.CONVERSATION)
  | public class Child
  | {
  |     Long id;
  |     Long version;
  |     String name;
  |     Parent parent;
  |     
  |     public Child(Long id, Long version, String name, Parent parent)
  |     {
  |         // TODO Auto-generated constructor stub
  |         this.id = id;
  |         this.version = version;
  |         this.name = name;
  |         this.parent = parent;
  |     }
  | 
  |     public Child() {}
  |     
  |     @Id @GeneratedValue
  |     public Long getId()
  |     {
  |         return id;
  |     }
  |     public void setId(Long id)
  |     {
  |         this.id = id;
  |     }
  | 
  |     public String getName()
  |     {
  |         return name;
  |     }
  |     public void setName(String name)
  |     {
  |         this.name = name;
  |     }
  |     
  |     @ManyToOne
  |     public Parent getParent()
  |     {
  |         return parent;
  |     }
  |     public void setParent(Parent parent)
  |     {
  |         this.parent = parent;
  |     }
  | 
  |     @Version
  |     public Long getVersion()
  |     {
  |         return version;
  |     }
  |     public void setVersion(Long version)
  |     {
  |         this.version = version;
  |     }
  | 
  |     public boolean equals(Object o)
  |     {
  |         if(o instanceof Child)
  |         {
  |             return o.toString().equals(this.toString());
  |         }
  |         return false;
  |     }
  |     
  |     public String toString()
  |     {
  |         return this.getClass() + "@" + name;
  |     }
  |     
  |     public int hashCode()
  |     {
  |         return this.toString().hashCode();
  |     }
  | }
  | 

Persistence.xml...


  | <persistence>
  | 	<persistence-unit name="testDatabase">
  |       <provider>org.hibernate.ejb.HibernatePersistence</provider>
  |       <jta-data-source>java:/DefaultDS</jta-data-source>
  |       <properties>
  |          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
  |          <property name="hibernate.show_sql" value="true"/>
  |          <!-- These are the default for JBoss EJB3, but not for HEM: -->
  |          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
  |          <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>         
  |          <property name="jboss.entity.manager.factory.jndi.name" value="java:/testEntityManagerFactory"/>         
  |       </properties>
  | 	</persistence-unit>	
  | 	<persistence-unit name="schoolDatabase">
  |       <provider>org.hibernate.ejb.HibernatePersistence</provider>
  |       <jta-data-source>java:/jdbc/OracleDS</jta-data-source>
  |       <properties>
  |          <property name="hibernate.hbm2ddl.auto" value="none"/>
  |          <property name="hibernate.show_sql" value="true"/>
  |          <!-- These are the default for JBoss EJB3, but not for HEM: -->
  |          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
  |          <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
  |          <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
  |       </properties>
  | 	</persistence-unit>
  | </persistence>
  | 

Components.xml...


  | <?xml version="1.0" encoding="UTF-8"?>
  | <components>
  | 	<component class="org.jboss.seam.core.Jbpm">
  |     	<property name="pageflowDefinitions">pageflow.jpdl.xml</property>
  | 	</component>
  |     <component name="em" class="org.jboss.seam.core.ManagedPersistenceContext">
  |         <property name="persistenceUnitJndiName">java:/testEntityManagerFactory</property>
  |     </component>	
  | </components>
  | 

ParentActions...


  | package sample.business.pc;
  | 
  | import javax.ejb.Stateless;
  | import javax.interceptor.Interceptors;
  | import javax.persistence.EntityManager;
  | 
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Out;
  | import org.jboss.seam.contexts.Contexts;
  | import org.jboss.seam.ejb.SeamInterceptor;
  | 
  | import sample.model.pc.Parent;
  | 
  | @Stateless
  | @Name("parentActions")
  | @Interceptors(SeamInterceptor.class)
  | public class ParentActionsImpl implements ParentActions
  | {
  |     @In(create=true)
  |     EntityManager em;
  |     
  |     @Out(required=false)
  |     Parent parent;
  |     
  |     @In(required=false)
  |     Parent newParent;
  |     
  |     public String createParent()
  |     {
  |         em.persist(newParent);
  |         parent = newParent;
  |         return "/pc/viewer.xhtml";
  |     }
  |     
  |     public Parent getParent(Long id)
  |     {
  |         return em.find(Parent.class, id);
  |     }
  | }
  | 

ChildListBean...


  | package sample.business.pc;
  | 
  | import java.util.List;
  | 
  | import javax.ejb.Remove;
  | import javax.ejb.Stateful;
  | import javax.persistence.EntityManager;
  | 
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Destroy;
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Scope;
  | import org.jboss.seam.annotations.datamodel.DataModel;
  | import org.jboss.seam.contexts.Contexts;
  | 
  | import sample.model.pc.Child;
  | import sample.model.pc.Parent;
  | 
  | @Stateful
  | @Name("childListBean")
  | @Scope(ScopeType.SESSION)
  | public class ChildListBeanImpl implements ChildListBean
  | {
  |     @In(create=true)
  |     EntityManager em;
  |     
  |     @In
  |     Parent parent;
  |     
  |     @DataModel
  |     List<Child> children;
  |         
  |     public void queryForChildrenForParent()
  |     {
  |         System.out.println(parent);
  |         System.out.println(em);
  |         if(parent != null)
  |         {
  |             children = em.createQuery("from Child c where c.parent = :parent").setParameter("parent", parent).getResultList();
  |         }
  |     }
  |     
  |     @Remove @Destroy
  |     public void destroy()
  |     {
  |         
  |     }
  | }
  | 

Pages.xml...


  | <pages>
  |   <page view-id="/activity/viewer.xhtml" action="#{activityActions.getActivitiesForUser}"/>
  |   <page view-id="/pc/viewer.xhtml" action="#{childListBean.queryForChildrenForParent}"/>
  | </pages>
  | 

createParent.xhtml


  | <html xmlns="http://www.w3.org/1999/xhtml"
  | 	    xmlns:f="http://java.sun.com/jsf/core"
  | 	    xmlns:h="http://java.sun.com/jsf/html"
  | 	    xmlns:ui="http://java.sun.com/jsf/facelets"
  | 	    xmlns:c="http://java.sun.com/jstl/core">
  |   <head>
  |     <title>Create Parent</title>
  |   </head>
  |   <body>
  |     <h:form>
  |       <table>
  |         <tr>
  |           <td>Name:</td>
  |           <td><h:inputText value="#{newParent.name}"/></td>
  |         </tr>
  |         <tr>
  |           <td>Submit:</td>
  |           <td><h:commandButton action="#{parentActions.createParent}"/></td>
  |         </tr>
  |       </table>      
  |     </h:form>
  |   </body>
  | </html>
  | 

viewer.xhtml


  | <html xmlns="http://www.w3.org/1999/xhtml"
  | 	    xmlns:f="http://java.sun.com/jsf/core"
  | 	    xmlns:h="http://java.sun.com/jsf/html"
  | 	    xmlns:ui="http://java.sun.com/jsf/facelets"
  | 	    xmlns:c="http://java.sun.com/jstl/core"
  | 	    xmlns:s="http://jboss.com/products/seam/taglib"
  | 	    xmlns:a="https://ajax4jsf.dev.java.net/ajax">
  |   <head>
  |     <title>Create Parent</title>
  |   </head>
  |   <body>
  |     <h:dataTable value="#{children}" var="tmp" rendered="#{!empty children}">
  |       <h:column>
  |         <f:facet name="header">Name</f:facet>
  |         #{tmp.name}
  |       </h:column>
  |       <h:column>
  |         <f:facet name="header">Edit</f:facet>
  |         <s:link value="Edit Child" action="#{childActions.editChild(tmp)}"/>
  |       </h:column>
  |     </h:dataTable>  
  |     <h:form>
  |       <h:commandButton action="#{childActions.createChild}"/>  
  |     </h:form>
  |   </body>
  | </html>
  | 

Problems are twofold...

1) The parent doesn't seem to be getting set into the session scope. I have no idea how I screwed that one up, since I've never had problems with it before.
2) Even though they're being injected the exact same way, the entity manager in ChildListBean is showing as null.

This is starting to drive me up a wall. :(

I think #2 has to do with the new way I'm dealing with the EntityManager... are you supposed to mix and match the EntityMangager injection and the @PersistenceContext injection?


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

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



More information about the jboss-user mailing list