1 Reply Latest reply on Jan 18, 2012 12:48 AM by william stephen

    Need Help h:selectOneMenu

    Nagendra krishnawat Newbie

      I have a dropdown list on UI:


      CustodianAccountCreate.xhtml code:


      <h:selectOneMenu value="#{custodianAccountHome.instance.bank}" style="width:110px" required="true" >
                              <s:selectItems value="#{allCustodians}" var="bankVar"  label="#{bankVar.name}" noSelectionLabel="Please Select.."/>
                          </h:selectOneMenu>



      Here allCustodians is variable from seam context as follows:


      @SuppressWarnings("unchecked")
          @Factory(value="allCustodians",scope=ScopeType.APPLICATION)
          public List<Bank> getAllCustodians(){
              Query q = getEntityManager().createQuery("select bank from Bank bank");
              List<Bank> list = q.getResultList();
              return list;
          }




      import java.util.ArrayList;
      import java.util.List;
      
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.framework.EntityHome;
      
      import com.xyz.party.Bank;
      import com.xyz.party.BankBranch;
      import com.xyz.party.Person;
      import com.xyz.portfolio.CustodianAccount;
      import com.xyz.portfolio.PortfolioType;
      
      
      @Name("custodianAccountHome")
      @Scope(ScopeType.CONVERSATION)
      public class CustodianAccountHome extends EntityHome<CustodianAccount> {
              
              @In     EntityManager entityManager;
              
              
              Person lendingAgent,accountingContact, tradeContact,cca ;
              
              private String spPortfolioType, isPortfolioType;
              
              public void setCustodianAccountId(Integer id) {
              setId(id);
          }
      
          public Integer getCustodianAccountId() {
              return (Integer) getId();
          }
          
          @Override
          public CustodianAccount getInstance() {
              return super.getInstance();
          }
      
              public Person getLendingAgent() {
                      return lendingAgent;
              }
      
              public void setLendingAgent(Person lendingAgent) {
                      this.lendingAgent = lendingAgent;
              }
      
              public Person getAccountingContact() {
                      return accountingContact;
              }
      
              public void setAccountingContact(Person accountingContact) {
                      this.accountingContact = accountingContact;
              }
              
              /**
           * Creates a list of entities which are to be displayed on "Search Preferences".
           * @return List
           */
              @Factory(value="allPortfolioTypeContext",scope=ScopeType.APPLICATION)
          public List<String[]> getPortfolioTypeList() {
              List<String[]> selectItems;
              List<PortfolioType> portfolioTypeList=  getEntityManager().createQuery("select portfolioType from PortfolioType portfolioType").getResultList();
              selectItems = new ArrayList<String[]>();
              selectItems.add(new String[]{"","Please Select"});
              for(int index=0;index<portfolioTypeList.size();index++){
                  PortfolioType portfolioType = portfolioTypeList.get(index);
                  selectItems.add(new String[]{portfolioType.getCode(),portfolioType.getName()});
              }
              return selectItems;
          }
      
              public Person getTradeContact() {
                      return tradeContact;
              }
      
              public void setTradeContact(Person tradeContact) {
                      this.tradeContact = tradeContact;
              }
      
              public Person getCca() {
                      return cca;
              }
      
              public void setCca(Person cca) {
                      this.cca = cca;
              }
      
              public String getSpPortfolioType() {
                      return spPortfolioType;
              }
      
              public void setSpPortfolioType(String spPortfolioType) {
                      this.spPortfolioType = spPortfolioType;
              }
      
              public String getIsPortfolioType() {
                      return isPortfolioType;
              }
      
              public void setIsPortfolioType(String isPortfolioType) {
                      this.isPortfolioType = isPortfolioType;
              }
              
      }
      



      Entity Classes are CustodianAccount and Bank, One Bank can have many CustodianAccount as follows:


      @Entity
      @Audited
      @NamedQueries( {
          @NamedQuery(name = "CustodianAccount.findByBankName", query = "select c from CustodianAccount c where c.bank.name=:name"),
          @NamedQuery(name = "CustodianAccount.findByAccountName", query = "select c from CustodianAccount c where c.name=:name"),
          @NamedQuery(name = "CustodianAccount.findByPFNumber", query = "select c from CustodianAccount c where c.portfolio.portfolioNumber=:portfolioNumber")
      })
      @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"portfolio", "bank", "primaryAccountNumber"}))
      public class CustodianAccount extends AbstractServiceAccount {
      
          /** Primary key id */
          @Id
          @SequenceGenerator(name = "CustodianAccount", sequenceName = "custodian_account_id")
          @GeneratedValue(generator = "CustodianAccount")
          private int id;
      
          @ManyToOne(optional=false)
          @ForeignKey(name = "FK_CA_bank")
          private Bank bank;
      
          @ManyToOne
          @ForeignKey(name = "FK_CA_bankbranch")
          private BankBranch branch;
      
          @OneToOne
          private ClientPortfolio portfolio;




      My aim is to create a new CustodianAccount using UI. On the UI user selects one Bank out of list of banks (h:selectOneMenu) and press save.


      I get following exception on click of save:


      /portfolio/CustodianAccountCreate.xhtml @24,121 value="#{custodianAccountHome.instance.bank}": java.lang.IllegalArgumentException: argument type mismatch 



      If I add s:convertEntity I get "value not valid"



      <h:selectOneMenu value="#{custodianAccountHome.instance.bank}" style="width:110px" required="true" >
                          <s:convertEntity/>
                              <s:selectItems value="#{allCustodians}" var="bankVar"  label="#{bankVar.name}" noSelectionLabel="Please Select.."/>
                          </h:selectOneMenu>




      I was able to do this functionnality by creating a int vatiable in CustodianAccountHome class and then getting the entity using entityManmager and saving it but this approach Doesn't seem to be very clean.


      Awaiting advice on this.