4 Replies Latest reply on Oct 31, 2008 5:38 AM by joblini

    Simple Two Table Example

    clerum

      Can someone point me to a simple example where add something simple like a user but also tie that user to an organization.


      All the tutorials and examples I have found are just creating a simple user. I've got that down but I'm having trouble seeing how to tie two objects together. My entity objects are created.



      @Entity
      @Name("user")
      @Table(name="user")
      public class User implements Serializable
      {
           private static final long serialVersionUID = -3128908747836433645L;
           private long id;
           private int version;
           private String username;
           private String password;
           private String firstname;
           private String lastname;
           private Organization organization;
           private Status status;
      
           @GeneratedValue
           @NotNull
           @Id
           public long getId() {
                return id;
           }
           public void setId(long id) 
           {
                this.id = id;
           }
           
           @Version
           @NotNull
           public int getVersion() 
           {
                return version;
           }
           public void setVersion(int version) 
           {
                this.version = version;
           }
           
           @Email
           @Length(min=3,max=75)
           @NotNull
           public String getUsername() 
           {
                return username;
           }
           public void setUsername(String username) 
           {
                this.username = username;
           }
           
           @Length(min=64,max=64)
           @NotNull
           public String getPassword() 
           {
                return password;
           }
           public void setPassword(String password) 
           {
                this.password = password;
           }
           
           @Length(min=1,max=75)
           @NotNull
           public String getFirstname() 
           {
                return firstname;
           }
           public void setFirstname(String firstname) 
           {
                this.firstname = firstname;
           }
           
           @Length(min=1,max=75)
           @NotNull
           public String getLastname() 
           {
                return lastname;
           }
           public void setLastname(String lastname) 
           {
                this.lastname = lastname;
           }
      
           @ManyToOne
           @JoinColumn(name = "organization_id", nullable = false)
           @NotNull
           public Organization getOrganization() {
                return organization;
           }
           public void setOrganization(Organization organization) {
                this.organization = organization;
           }
           
           @NotNull
           public Status getStatus() {
                return status;
           }
           public void setStatus(Status status) {
                this.status = status;
           }
      }
      
      
      ---
      
      @Entity
      @Name("organization")
      @Table(name="organization")
      public class Organization implements Serializable
      {
           private static final long serialVersionUID = -5182241903501025893L;
           private long id;
           private int version;
           private String name;
           private Set<User> users = new HashSet<User>(0);
           
           @GeneratedValue
           @NotNull
           @Id
           public long getId() {
                return id;
           }
           public void setId(long id) {
                this.id = id;
           }
           
           @Version
           @NotNull
           public int getVersion() {
                return version;
           }
           public void setVersion(int version) {
                this.version = version;
           }
           
           @Length(min=1,max=75)
           @NotNull
           public String getName() {
                return name;
           }
           public void setName(String name) {
                this.name = name;
           }
           
           @OneToMany(mappedBy="id")
           public Set<User> getUsers() {
                return users;
           }
           public void setUsers(Set<User> users) {
                this.users = users;
           }
           
      }
      
      ---
      
      public enum Status {
           ACTIVE, DISABLED
      }
      
      



      But I'm having trouble grasping the best way to handle this from the action objects and the view .xhtml pages.



        • 1. Re: Simple Two Table Example
          clerum

          bump

          • 2. Re: Simple Two Table Example
            clerum

            bump 2

            • 3. Re: Simple Two Table Example
              joblini

              Maybe this can give you some ideas ...


              package org.agritrace.action.production;
              
              import java.util.ArrayList;
              import java.util.Iterator;
              import java.util.List;
              
              import org.agritrace.action.BaseEntityListController;
              import org.agritrace.action.stakeholder.StakeholderHome;
              import org.agritrace.model.production.Production;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Transactional;
              
              @Name("productionListController")
              public class ProductionListController extends BaseEntityListController<Production> {
              
                   private static final long serialVersionUID = 1L;
              
                   @In(create = true)
                   private ProductionHome productionHome;
              
                   @In
                   private StakeholderHome stakeholderHome;
              
                   private Production newProduction = new Production();
              
                   public List<Production> getEntityList() {
                        
                        return stakeholderHome.getInstance().getProductions();
                   }
                   
                   public List<Production> getResultList() {
                        
                        ArrayList<Production> list = new ArrayList<Production>(getEntityList());
                        list.add(newProduction);
                        return list;
                   }
                   
                   @Transactional
                   public String update() {
              
                        if (!newProduction.isEmpty()) {
                             stakeholderHome.getInstance().getProductions().add(newProduction);
                             newProduction.setStakeholder(stakeholderHome.getInstance());
                        }
              
                        processDiscontinuities(getEntityList());
                        
                        String outcome = stakeholderHome.update();
                        stakeholderHome.refresh();
                        
                        newProduction = new Production();
                        return outcome;
                   }
              
                   @Transactional
                   public String remove() {
              
                        String outcome = null;
              
                        List<Production> productionList = stakeholderHome.getInstance().getProductions();
                        for (Iterator<Production> i = productionList.iterator(); i.hasNext();) {
                             Production production = i.next();
                             if (production.isSelected()) {
                                  i.remove();
                                  productionHome.setInstance(production);
                                  productionHome.remove();
                             }
                        }
                        return outcome;
                   }
              
              }




              <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              
              <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                   xmlns:s="http://jboss.com/products/seam/taglib"
                   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:rich="http://richfaces.org/rich"
                   xmlns:a4j="http://richfaces.org/a4j"
                   template="/intranet/stakeholder/StakeholderTabs.xhtml">
              
                   <ui:define name="ProductionList">
              
                        <div id="stakeholderProductions">
                        
                             <h:form     id="listForm">
              
                             <rich:dataTable id="entityList"
                                             value="#{productionListController.resultList}" 
                                               var="production"
                                  onRowMouseOver="this.style.backgroundColor='#{a4jSkin.additionalBackgroundColor}'"
                                   onRowMouseOut="this.style.backgroundColor=''" 
                                       rowClasses="rvgRowOne,rvgRowTwo"
                                   columnClasses="tblSmallCols,tblCols,tblCols,tblSmallCols">
                        
                                  <a4j:support event="onRowDblClick" action="#{productionListController.onRowDblClick(production)}" reRender="entityList,commonFramesDiv" limitToList="false" ajaxsingle="true" />
              
                   
                                       <!-- 
                                       <rich:column width="50px" >
                                       <f:facet name="header">id</f:facet>
                                       <h:outputText value="#{production.id}"/>
                                       </rich:column>
                                        -->
                                        
                                       <rich:column styleClass="#{production.selected ? 'selected' : 'normal'}">
                                       
                                            <f:facet name="header">#{messages['org.agritrace.ProductionType']}<span class="required">*</span></f:facet>
                                       
                                            <s:decorate template="/layout/editTd.xhtml">
                                                 <h:selectOneMenu id="productionTypeSelect" value="#{production.type}" required="true"   
                                                                     converter="#{org.agritrace.productionTypeConverter}"
                                                                     label="#{messages['org.agritrace.ProductionType']}" >
                                                      <s:selectItems value="#{productionTypeList.resultList}"     var="productionType"
                                                           label="#{messages[productionType.resourceKey]}"
                                                           noSelectionLabel="#{messages['org.agritrace.PleaseSelect']}" hideNoSelectionLabel="true"  />
                                                 </h:selectOneMenu>
                                            </s:decorate>
              
                                       </rich:column>
                        
                                       <rich:column styleClass="#{production.selected ? 'selected' : 'normal'}">
              
                                            <f:facet name="header">#{messages['org.agritrace.Species']}<span class="required">*</span></f:facet>
                                            
                                            <s:decorate template="/layout/editTd.xhtml">
                                                 <h:selectOneMenu id="speciesSelect" value="#{production.species}" required="true"
                                                      converter="org.agritrace.speciesConverter"
                                                      label="#{messages['org.agritrace.productionSpecies']}"> 
                                                      <s:selectItems value="#{speciesList.resultList}" var="species"
                                                           label="#{messages[species.resourceKey]}"
                                                           noSelectionLabel="#{production.isEmpty()?messages['org.agritrace.PleaseSelect']:null}" />
                                                 </h:selectOneMenu>
                                            </s:decorate>
              
                                       </rich:column>
              
               
                                  </rich:dataTable>
                        
                                  <br />
                             
                                  <ui:include src="/intranet/common/list/ActionButtons.xhtml" >
                                       <ui:param name="controller" value="#{productionListController}" />
                                  </ui:include>
                        
                             </h:form>
                   
                        </div>
              
                        <br/>
                   
                        <s:div id="commonFramesDiv">
                             <ui:include src="/intranet/common/commonFrames/TabPanel.xhtml" />
                        </s:div>
              
                   </ui:define>
              
              </ui:composition>
              



              <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib" 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:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" >
              
                   <div class="actionButtons">
                        <h:commandButton id="update" value="#{messages['org.agritrace.Save']}" action="#{controller.update}" />
                        <h:commandButton id="delete" value="#{messages['org.agritrace.Delete']}" action="#{controller.remove}"     />
                   </div>
              
              </ui:composition>




              <?xml version="1.0" encoding="UTF-8"?>
              <page xmlns="http://jboss.com/products/seam/pages"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd"
                    no-conversation-view-id="/intranet/ProductionList.seam"
                    login-required="false">
                 
                   <begin-conversation flush-mode="MANUAL" join="true"/>
                 
                   <action execute="#{stakeholderHome.wire}" />
                   <action execute="#{productionListController.wire}" />
              
                   <param name="stakeholderId" value="#{stakeholderHome.stakeholderId}" />
               
              </page>
              



              • 4. Re: Simple Two Table Example
                joblini

                By extending EntityController (Seam framework component), you can define actions to manage the relationship between the two tables.