我有两张简单的桌子。
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);
}
}
这是我的数据表代码:
<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>