I have written about this topic in my blog(
http://farmerinchina.blog.sohu.com/46978657.html), and now paste here for some discussion.
In some situation, people need to create tables at runtime, so it is hard to map the table to pojo. In my project, I just need to display the content of new table, and then I use the following solution. But I think it has more meaning than what past here.
Page:
<t:dataTable id="recorddata"
var="record"
value="#{tableBean.recordModel}"
preserveDataModel="false"
rows="100"
>
<t:columns value="#{tableBean.headerModel}" var="columnHeader">
<f:facet name="header">
<h:outputText value="#{columnHeader.cname}" />
</f:facet>
<h:outputText value="#{tableBean.columnValue}" />
</t:columns>
</t:dataTable>
Backing Bean:
public class TableBean{
private DataModel recordModel;
private DataModel headerModel;
private int labelIndex = 0;
public DataModel getHeaderModel()
{
if(headerModel == null)
{
headerModel =new ListDataModel();
headerModel.setWrappedData(getColumnHeaderList());
}
return headerModel;
}
public DataModel getRecordModel() {
if(recordModel== null)
{
recordModel=new ListDataModel();
recordModel.setWrappedData(getRecordList());
}
return recordModel;
}
public String getColumnValue()
{
if(recordModel == null) recordModel = getRecordModel();
if(recordModel.isRowAvailable())
{
Map map = (Map)recordModel.getRowData();
List list = getColumnHeaderList();
String key = list.get(labelIndex)).toString();
Object o = map.get(key);
if(o == null) o = "";
labelIndex++;
if(labelIndex == map.size()) labelIndex = 0;
return o.toString();
}
return null;
}
}
DAO:
public class TableDAO{
/*******Get the content of dynamic created table******/
public List getRecordList(String tablename)
{
SQLQuery query = session.createSQLQuery("select * from "+tablename);
List list = query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.list();//transform resultset to map
return list;
}
/*******Get column header**********/
public List getRecordHeaderList()
{
}
}
I use hibernate-3.2, and myfaces-1.1.5