I have small problem with view datas in h:dataTable
item. I have native query which is working properly in database and in java:
SELECT SUM(price_list.first_class_price), SUM(price_list.second_class_price)
FROM price_list, connections
WHERE connections.id = price_list.id_connect
GROUP BY connections.source;
The method in EJB returns a List of elements. This method looks like this:
public List<PriceList> getFirstClassPrices() {
Query q = em.createNativeQuery("SELECT SUM(price_list.first_class_price), SUM(price_list.second_class_price) FROM price_list, connections WHERE connections.id = price_list.id_connect GROUP BY connections.source");
return q.getResultList();
}
I want to show result of this query in dataTableItem, but I don`t know how to get to this values. I tried to do something like this (FCP is the method in JSF ManagedBean that returns mentioned list):
<h:dataTable value="#{priceList.FCP}}" var="item" cellspacing="0" cellpadding="1" border="1">
<h:column>
<f:facet name="header">
<h: outputText value="First class"/>
</f:facet>
<h: outputText value="#{item}"/>
</h:column>
<h:column>
<f:facet name="header">
<h: outputText value="Second class"/>
</f:facet>
<h: outputText value="#{item}"/>
</h:column>
</h:dataTable>
but this shows me only references as the result in table.
FCP method in JSF ManagedBean looks like this:
public List<PriceList> getFCP() {
return priceListFacade.getFirstClassPrices();
}
This solution is working properly when I have a query like this:
SELECT SUM(price_list.first_class_price)
FROM price_list, connections
WHERE connections.id = price_list.id_connect
GROUP BY connections.source;
with one SUM element. Then correct datas are in the table and there is no problem.
My question is: how to get datas, no references from the result list when I have two or more SUM elements in a query?
I tried use code like this:
<h:dataTable value="#{priceList.FCP}}" var="item" cellspacing="0" cellpadding="1" border="1">
<h:column>
<f:facet name="header">
<h: outputText value="First class"/>
</f:facet>
<h: outputText value="#{item.first_class_price}"/>
</h:column>
<h:column>
<f:facet name="header">
<h: outputText value="Second class"/>
</f:facet>
<h: outputText value="#{item.second_class_price}"/>
</h:column>
</h:dataTable>
but then I have an EJB exception: there is no properties like first_class_price
and second_class_price
.
I tried to change the query for this:
SELECT SUM(price_list.first_class_price) AS p1, SUM(price_list.second_class_price) AS p2
FROM price_list, connections
WHERE connections.id = price_list.id_connect
GROUP BY connections.source;
and then in JSF I used a code like this:
<h:dataTable value="#{priceList.FCP}}" var="item" cellspacing="0" cellpadding="1" border="1">
<h:column>
<f:facet name="header">
<h: outputText value="First class"/>
</f:facet>
<h: outputText value="#{item.p1}"/>
</h:column>
<h:column>
<f:facet name="header">
<h: outputText value="Second class"/>
</f:facet>
<h: outputText value="#{item.p2}"/>
</h:column>
</h:dataTable>
but I have the same EJB exception that properties p1 and p2 doesn't exists.
I am new in Java EE and I can't handle with this. Maybe there is another way to solve my problem? Maybe I shouldn't use h:dataTableItem
?
Thanks for your replies.
I have found a solution of my problem on the other topic. Here is the right solution:
SessionBean:
public List<Object[]> getFirstClassPrices() {
Query q = em.createNativeQuery("SELECT SUM(price_list.first_class_price), "
+ "SUM(price_list.second_class_price), SUM(price_list.third_class_price), "
+ "SUM(price_list.luggage_fee), SUM(price_list.airport_fee), SUM(price_list.fuel_fee), "
+ "SUM(price_list.crew_salary), connections.source FROM price_list, connections WHERE connections.id = price_list.id_connect GROUP BY connections.source");
List<Object[]> lista = q.getResultList();
return lista;
}
JSF ManagedBean:
public List<Object[]> getFCP() {
return priceListFacade.getFirstClassPrices();
}
and finally fragment of JSF page:
<h:outputText value="#{item[0]}"/>
I got stuck when trying to use "GROUP BY" query in my application. Solution I used is following:
1, tableNamefacade.java:
public List<Object[]> getAllGroupByCoulumn() {
Query q = em.createNativeQuery("SELECT d.column FROM tableName d GROUP BY d.coulmn");
List<Object[]> list = q.getResultList();
return list;
}
2, tableNameController.java: property of bean:
private List<Object[]> items;
and function, which prepare list that is displayed at xhtml page:
items = getFacade().getAllGroupByCoulumnm();
3, finaly the dataTable in xhtml page:
<h:dataTable value="#{myController.items}" var="item">
...
<h:outputText value="#{item}">
...
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.