0 Replies Latest reply on Jun 18, 2010 1:47 PM by Chris Feller

    How to handle collections

    Chris Feller Newbie

      Hi,
      I need some help with collections.

      I have 2 entities employee and journey.
      An employee is doing journeys...
      I want to store the journeys that the employee is doing in a set.


      My questions are:
      How does the action method looks like to persist a new journey to a logged in employee?
      And how does the xhtml-view looks like where the employee can enter a new journey?

      How does the action method looks like to get the journeys?
      And how does the xhtml-view looks like where i can view all the journeys that a employee has done?

      Thanks for your support.

      ...I have tried something like this:



      @Entity
      @Scope(SESSION)
      @Name("employee")
      public class Employee implements Serializable {
           
           private long employeeId;
           private String username;
           private String passwordHash;
           private Set<Journey> journeys =new HashSet<Journey>();
           
           public Employee() { super(); }
           
           @Id @GeneratedValue
           public long getEmployeeId() { return employeeId; }
              public void setEmployeeId(long employeeId) { this.employeeId = employeeId; }
        
              //more  getter/setter...

           @OneToMany(mappedBy="employee", cascade=CascadeType.ALL)
           public Set<Journey> getJourneys() { return this.journeys; }
           public void setJourneys(Set<Journey> journeys) { this.journeys = journeys; }

      }


      @Entity
      @Scope(SESSION)
      @Name("journey")
      public class Journey implements Serializable{
        
         private long journeyId;
         private String destination;
         //...
         private Employee employee = new Employee();
        
         public Journey() { super(); }
        
         @Id
         @GeneratedValue
         public long getJourneyId() { return journeyId; }
         public void setJourneyId(long journeyId) { this.journeyId = journeyId; }
        
          //more  getter/setter...  

         @ManyToOne(cascade=CascadeType.ALL)
         @JoinColumn(name="employeeId")
         public Employee getEmployee() { return employee; }
         public void setEmployee(Employee employee) { this.employee = employee; }

      }  


      // Action to persist a journey
      @Stateful
      @Name("persistjourney")
      public class PersistJourneyAction implements PersistJourney{
           
           @In
           private Journey journey;
           
           @In
           private Employee employee;

           @PersistenceContext
           private EntityManager em;
           
           public String persist () {
                   //set relationship
                this.journey.setEmployee(this.employee);
                   this.employee.getJourneys().add(this.journey);
                   //persist data, because 1:1 relation via merge of employee
                em.merge(this.employee);
                   return "/login.xhtml";
           }
           
           @Destroy
           @Remove
           public void destroy() {
           }
           
      }


      //view to enter a new journey
      <h:form>
            <div>
               <h2>Journey-Data</h2>
                  <h:panelGrid columns ="2">
                     <h:outputLabel value="Destination:"/>
                     <h:inputText value="#{journey.destination}" />
                  </h:panelGrid>
             </div>
             <h:commandButton type="submit" value="OK"
                       action="#{persistjourney.persist()}" style=" width : 98px;"/>
      </h:form>