简体   繁体   中英

Join two tables in DataTable hibernate

I have two simple tables.

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

i can display in datatable (primeface) all columns of Table Projet but i can"t figure out how i can get FirstName and lastname from Table CompteChefs i would like to display data from two tables in a DataTable like this :

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

so to do that i used to do like this :

@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);
    }
}

and this my DataTable code :

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

i use Hibernate Mapping to Join Tables

in Projets.hbm.xhtml :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM