如何在jsf中将商店实体合并到一个客户实体(多对一)

时间:2013-02-13 06:09:27

标签: jsf jpa ejb facelets

我正在尝试将现有商店合并到jsf中的新客户端,但这并不成功。该程序基本上由一个支持bean控制器,一个ejb和jsf页面(寄存器)组成,我已经能够在一个comboBox UI中填充shoplist。这是代码。

register.xhtml:

clientcontroller.client.fname是SFSB Property已经存在,但试图合并 列表中的商店(shopcontroller.shopList

<h:form>
<h:panelGrid columns="3" >
<h:outputText value="Select From.  
 Available    Shops :" />     
 <h:selectOneMenu value="#. 
 {shopController.shop}" >
 <f:selectItems var="s" value="#.    
 {shopController.shopList}" />   
  </h:selectOneMenu>

 <h:commandButton value="register".   
  action="#{clientcontroller.Register(s)}" />
 </h:panelGrid>
</h:form> 

支持bean类:

ManagedBean(name="clientcontroller")
@RequestScoped
public class clientController {

@EJB ClientEJB clientEJB;

 private Client clt = new Client();
 private Shop shp = new Shop();
 private String clientfname;

//getters and setters

public String Register(Shop shp){
  this.shp = shp;
  clientEJB.register(clt, shp);
  return ""; 
}

EJB类:

@Stateful
@LocalBean
public class ClientEJB {

  @PersistenceContext 
  EntityManager em;


  public void addClient(Client clt){
    em.persist(clt);
  }

  public void register(Client c ,Shop s){
    c.getShopList().add(s);
    s.setAvailability("false");
    s.setClientid(c);
    em.merge(s);
    em.merge(c);
  }
}

1 个答案:

答案 0 :(得分:0)

按如下方式调整代码:

XHTML:

  <h:commandButton value="register" action="#{clientcontroller.Register}" />

ManagedBean

  public String Register(){
      clientEJB.register(clt, shp);
      return ""; 
  }

另见: