在DataTable hibernate中加入两个表

时间:2013-06-19 20:25:06

标签: java hibernate primefaces datatable

我有两个简单的表格。

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)中显示Table Projet的所有列,但我无法弄清楚如何从Table CompteChefs获取FirstName和lastname 我想在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>

1 个答案:

答案 0 :(得分:0)

我使用Hibernate Mapping连接表

在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>