[jboss-user] [JBoss Seam] - Re: Entities with one/many_to_many relationships and SEAM

adam.stortz do-not-reply at jboss.com
Thu Aug 24 08:32:29 EDT 2006


Trouby,

Yes, I am using JBoss with JSF 1.2_01 RI and facelets

I am using JBoss 4.0.4 with the EJB 3.0 RC8 update

I am not using the multiple SelectItems to populate a ManyToMany or OneToMany relationship, I am injecting the selected items into a stateful session bean, but translating it to manage an entity relationship would be fairly easy.  Here are the important pieces of code, maybe another example would help you.  The most interesting parts will be in bold.

editBlogEntry.xhtml

  | <ui:composition template="/template/template.xhtml"
  |     xmlns="http://www.w3.org/1999/xhtml"
  |       xmlns:ui="http://java.sun.com/jsf/facelets"
  |       xmlns:f="http://java.sun.com/jsf/core"
  |       xmlns:h="http://java.sun.com/jsf/html"
  |      xmlns:selectitems="http://jboss.com/products/seam/selectitems/taglib">
  |        
  |         <ui:define name="body">
  |             <h:messages globalOnly="true" layout="list" />
  |             
  |             <h:form>
  |                	<h:inputHidden value="${blogEntry.id}" />
  |                 <h:panelGrid columns="3">
  |                 
  |                     <h:outputText value="${msgs.title}:" />
  |                     <h:inputText id="title" value="${blogEntry.title}" required="true"/>
  |                     <h:message for="title" styleClass="error"/>
  |                 
  |                     <h:outputText value="${msgs.body}:" />
  |                     <h:inputTextarea id="body" value="${blogEntry.body}" rows="50" cols="30"/>
  |                     <h:message for="body" styleClass="error"/>
  |                 
  |                     <h:outputText value="${msgs.userGroups}:"/>
  | 					<h:outputText value="${msgs.userGroupsEmpty}" rendered="${empty userGroupsSelectItems}" />
  |                 	<h:selectManyListbox id="userGroups" size="5" value="${blogEntryService.selectedUserGroups}" rendered="${not empty userGroupsSelectItems}">
  | 						<f:selectItems value="${userGroupsSelectItems}"/>
  | 						<selectitems:convertEntity entityClass="org.unleashed.ejb.entity.UserGroup" />
  | 					</h:selectManyListbox>
  | 				    <h:message for="userGroups" styleClass="error" rendered="${not empty userGroupsSelectItems}"/>
  |                 
  |                 </h:panelGrid>
  |                 
  |                 <h:commandButton action="${blogEntryService.save}" value="${msgs.save}" />
  |             </h:form>
  | 
  |         </ui:define>
  | </ui:composition>
  | 

BlogEntryServiceBean.java - This is where the multiple select items get injected from the page

  | @Stateful
  | @Name("blogEntryService")
  | public class BlogEntryServiceBean implements BlogEntryService{
  | 	
  | 
  | 	@Logger private Log log;
  | 	
  | 	@PersistenceContext
  | 	private EntityManager em;
  | 	
  | 	@In (create=true)
  | 	@Out
  |     @Valid
  | 	private BlogEntry blogEntry;
  | 
  |     
  | 	@SelectItems(label="name")
  | 	private List<UserGroup> userGroupsSelectItems;
  | 	
  | 	private List<UserGroup> selectedUserGroups;
  | 	    
  |     @Factory("userGroupsSelectItems")
  | 	public List<UserGroup> findAllUserGroupsSelectItems()
  | 	{
  |     	userGroupsSelectItems = em.createQuery("from UserGroup userGroup").getResultList();
  | 		return userGroupsSelectItems;
  | 	}
  |     
  |     @IfInvalid(outcome=Outcome.REDISPLAY)
  | 	public String save()
  | 	{
  |     	log.debug("Selected User Groups: '" + selectedUserGroups + "'");
  |     	blogEntry.setPostDate(new Date());
  |     	log.debug("Saving Blog Entry: " + blogEntry);
  | 		em.merge(blogEntry);
  |     	log.debug("Blog Entry: " + blogEntry + " saved.");
  | 		return "viewBlogEntries";
  | 	}
  |     
  |     public List<UserGroup> getSelectedUserGroups()
  |     {
  |     	return selectedUserGroups;
  |     }
  |     public void setSelectedUserGroups(List<UserGroup> selectedUserGroups)
  |     {
  |     	this.selectedUserGroups = selectedUserGroups;
  |     }
  | ...	
  | 	
  | }
  | 
  | 

In the save method, the selected items are logged, right now I am performing no actions based on the selection, but the proper output shows up in the log

components.xml - Deployed in WEB-INF
FYI unleashed is the name of my application

  | <components>
  | 
  | 	<component class="org.jboss.seam.selectitems.SelectItemsPersistenceConfig">
  | 	   <property name="persistenceUnitJndiName">java:/selectItemsEntityManagerFactory</property>
  | 	</component>
  | 
  | 	<component name="em" class="org.jboss.seam.core.ManagedPersistenceContext">
  |         <property name="persistenceUnitJndiName">java:/unleashedEntityManagerFactory</property>
  |     </component>
  | 
  |     <component class="org.jboss.seam.selectitems.SelectItemsPersistenceConfig">
  |     	<property name="persistenceUnitJndiName">java:/unleashedEntityManagerFactory</property>
  |     </component>
  | </components>
  | 

I also had to override the equals method in my UserGroup entity:

  |                 @Override
  | 	public boolean equals(Object obj) {
  | 		if (obj instanceof UserGroup) {
  | 			UserGroup userGroup = (UserGroup) obj;
  | 			return this.id == userGroup.getId();
  | 		} else {
  | 			return false;
  | 		}
  |                 }
  | 

I think that covers all of the code that has any impact on that.  If you have any questions about the code, please let me know.

Regards,
Adam

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

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



More information about the jboss-user mailing list