2013-06-19 54 views
0

我有兩個簡單的表。在DataTable中加入兩張表hibernate

Table : Projet 

+----+----------+---------+-----------+----------+----------+ 
| ID | RefProjet|NomPr | NomDocs | Docs  | IDChef | 
+----+----------+---------+-----------+----------+----------+ 
| 1 | Rf-1  | Projet1 | ChampsLivF| Blob | 1  | 
| 2 | Rf-2  | Projet2 | CreeAcM | Blob | 2  | 
+----+----------+---------+-----------+----------+----------+ 

Table : ComptesChefs 
+---------+----------+-----------+ 
| IDChef | NomChef |PrenomChef | 
+---------+----------+-----------+ 
| 1  | Dany  | Smith  | 
| 2  | Sami  | Tridi  | 
+---------+----------+-----------+ 

我可以在數據表(primeface)顯示錶謨的所有列,但我可以「T弄清楚如何我可以從表CompteChefs 獲得名字和姓氏的我想在一個DataTable中顯示來自兩個表中的數據像這樣:

+----------+---------+-----------+----------+----------+------------+ 
| RefProjet|NomPr | NomDocs | Docs  | NomChef |PrenomChef | 
+------ ---+---------+-----------+----------+----------+------------+ 
| Rf-1  | Projet1 | ChampsLivF| Link | Dany  | Smith  | 
| Rf-2  | Projet2 | CreeAcM | Link | Sami  | Tridi  | 
+----------+-----+-----------+----------+---+----------+------------+ 

這樣做我以前做的是這樣的:

@ManagedBean 
@RequestScoped 
public class affichPrj { 
private String matv; 

private List<Projets> listPrj=new ArrayList<Projets>(); 
private DataModel<Projets> data =new ListDataModel<Projets>(); 
private Projets selectedprj=new Projets(); 
    /** Creates a new instance of affichPrj */ 
    public affichPrj() { 
     Session se=geoUtil.getSessionFactory().getCurrentSession(); 
     Transaction tr=se.beginTransaction(); 
     Query q=se.createQuery("from Projets "); 
     listPrj=q.list(); 
     data.setWrappedData(listPrj); 
    } 

    public DataModel<Projets> getData() { 
     data.setWrappedData(listPrj); 
     return data; 
    } 

    public void setData(DataModel<Projets> data) { 
     this.data = data; 
    } 

    public List<Projets> getlistPrj() { 
     return listPrj; 
    } 

    public void setlistPrj(List<Projets> listPrj) { 
     this.listPrj = listPrj; 
    } 



    public Projets getselectedprj() { 
     selectedprj=(Projets)data.getRowData(); 
     return selectedprj; 
    } 

    public void setselectedprj(Projets selectedprj) { 
     this.selectedprj = selectedprj; 
    } 
    public void editeprj(AjaxBehaviorEvent event){ 
     selectedprj=(Projets)data.getRowData(); 
     Session session = geoUtil.getSessionFactory().openSession(); 
Transaction tr = session.beginTransaction(); 
Projets v= (Projets)session.get(Projets.class, selectedprj.getIdpro()); 
v=selectedprj; 
    session.merge(v); 
    tr.commit(); 
     int i=0; 
     boolean ok=false; 
     while(i<listPrj.size() && true==false){ 

      if(listPrj.get(i).getIdpro().equals(selectedprj.getIdpro())){ 
       ok=true; 
       listPrj.remove(i); 
       listPrj.add(i, v); 
      } 
      i++; 
     } 
    data.setWrappedData(listPrj); 

    } 
    public void deleteprj(AjaxBehaviorEvent event){ 
    selectedprj=data.getRowData(); 
     Session se=geoUtil.getSessionFactory().getCurrentSession(); 
     Transaction tr=se.beginTransaction(); 
     se.delete(selectedprj); 
     tr.commit(); 
    listPrj.remove(this.selectedprj); 
     data.setWrappedData(listPrj); 
    } 
} 

,這我的DataTable代碼:

 <p:column filterBy="#{cmd.idpro}" sortBy="#{vh.matV}"> 
     <f:facet name="header"> 
      <h:outputText value="Id_Pro" /> 
     </f:facet> 
      <h:outputText value="#{cmd.idpro}" /> 
    </p:column> 

    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Ref_Proj" /> 
     </f:facet> 
     <h:outputText value="#{cmd.idProjet}" /> 
    </p:column> 
    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Nom Porjet" /> 
     </f:facet> 
     <h:outputText value="#{cmd.nomProjet}" /> 
    </p:column> 
     <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Date debut" /> 
     </f:facet> 
     <h:outputText value="#{cmd.dateDeb}" /> 
    </p:column> 
    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Date Fin" /> 
     </f:facet> 
     <h:outputText value="#{cmd.dateFin}" /> 
    </p:column> 
    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Nom Chef" /> 
     </f:facet> 
     <h:outputText value="#{???}" /> << == here my Prblm ?!! how to get the name 
    </p:column> 
    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Prenom Chef" /> 
     </f:facet> 
     <h:outputText value="#{???}" /> << == and here my Prblm also?!! 
    </p:column> 
    <p:column> 
     <f:facet name="header"> 
      <h:outputText value="Nom Image" /> 
     </f:facet> 
      <h:outputText value="#{cmd.nomimg}" /> 
    </p:column> 
    <p:column style="width:32px"> 
     <p:commandLink update=":f:display" id="link" oncomplete="carDialog.show()" > 
      <img src="images/edit.jpg"></img> 
      <f:setPropertyActionListener target="#{affichPrj.selectedprj}" value="#{cmd}" /> 
     </p:commandLink> 
      </p:column> 
     <p:column style="width:32px"> 
      <h:commandLink action="#{affichPrj.deleteprj}" > 
      <img src="images/delete.jpg"></img> 
      <f:ajax listener="#{affichPrj.deleteprj}"></f:ajax> 
     </h:commandLink> 


    </p:column> 

</p:dataTable> 

回答

0

我使用Hibernate映射到聯接表

在Projets.hbm.xhtml:

<many-to-one name="compte" class="com.persistence.Compte" fetch="select"> 
      <column name="IdChef" not-null="true" /> 
    </many-to-one> 
在Compte.hbm.xml

<set name="projetChef" table="projet" inverse="true" lazy="true" fetch="select"> 
      <key> 
       <column name="IdChef" not-null="true" /> 
      </key> 
     <one-to-many class="com.persistence.Projets" /> 
</set>