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

    How to handle collections

    Chris Feller Newbie

      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:

      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; }


      public class Journey implements Serializable{
         private long journeyId;
         private String destination;
         private Employee employee = new Employee();
         public Journey() { super(); }
         public long getJourneyId() { return journeyId; }
         public void setJourneyId(long journeyId) { this.journeyId = journeyId; }
          //more  getter/setter...  

         public Employee getEmployee() { return employee; }
         public void setEmployee(Employee employee) { this.employee = employee; }


      // Action to persist a journey
      public class PersistJourneyAction implements PersistJourney{
           private Journey journey;
           private Employee employee;

           private EntityManager em;
           public String persist () {
                   //set relationship
                   //persist data, because 1:1 relation via merge of employee
                   return "/login.xhtml";
           public void destroy() {

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