1 Reply Latest reply on Apr 27, 2010 5:34 PM by Kevin Sapper

    Help required in passing primary key for getting list of values corresponding to key

    vasuki hn Newbie

      Hello,


      I have written 2 entity files(Customer.java and Location.java) and coressponding few files that are


      CustomerHome.java,       LocationHome.java
      CustomerList.java,       LocationList.java
      Customer.xhtml,          Location.xhtml
      CustomerEdit.xhtml,      LocationEdit.xhtml
      CustomerList.xhtml,      LocationList.xhtml


      I want to pass primary key from home.xhtml


      I want to know how to pass primary key from .xhtml to .java file so that i will get only those list of data which corresponds to the passed primary key.


      I will explain a scenario to better understand my problem:
      In my home.xhtml file, i am listing list of customers and when i click on any one of the customers, it should show me a list of locations corresponding to that customer.


      So how to pass the primary key(customerId) from home.xhtml to list the location?



      Thanks
      Vasuki

        • 1. Re: Help required in passing primary key for getting list of values corresponding to key
          Kevin Sapper Newbie

          Hi Vasuki,


          a possible solution for your problem is to create a list and push it via @DataModel to your view. If an action is executed on the table the selected customer is automatically pushed into the beans selecedCustomer attribute.
          And I guess that your entites have a relationship so it should be no problem for you to obtain the location data from a customer entity like the function 'showLocations' does.


          @Name("customerAction")
          @Scope(ScopeType.SESSION)
          public class CustomerAction implements Serializable {
          
              @In
              private EntityManager entityManager;
          
              @DataModel
              private List<Customer> customerList;
              @DataModelSelection(value = "customerList")
              @Out(required = false)
              private Customer selectedCustomer;
          
              @DataModel
              private List<Location> locationList; 
              
              @Factory(value="customerList")
              public List<Customer> initCustomerList() {
                  return (ArrayList<Customer>)
                    entityManager.createQuery("SELECT e FROM EnglishDictionary e")
                      .getResultList();
              }
          
              public void showLocations() {
                  locationList = selectedCustomer.getLocations();
              }
          


          To display the location result in your home.xhtml you wont render the locationsPanel until a customer has been selected and his locations are loaded.


          <rich:panel id="custerPanel">
            <rich:dataTable id="customerTable" value="#{customerList}" var="_customer">
              <rich:column>
                <f:facet value="header">Customer name</f:facet>
                <h:outputText value="#{_customer.name}" />
              </rich:column>
              <rich:column>
                <f:facet> value="header">Action</f:facet>
                <h:commandButton action="#{customerAction.showLocation}" />
              </rich:column>
            </rich:dataTable>
          </rich:panel>
          
          <rich:panel id="locationPanel" rendered="#{not empty locationList}">
            <f:facet name="header">selectedCustomer.name</f:facet>
            <rich:dataTable id="locationTable" value="#{locationList}" var="_location"}">
              <rich:column>
                <f:facet value="header">Location name</f:facet>
                <h:outputText value="#{_location.name}" />
              </rich:column>
            </rich:dataTable>
          </rich:panel>
          



          //Kevin