Forums Register Login

max function showing error

+Pie Number of slices to send: Send
Hi,
I am trying to execute 'max' function in hql but getting error--

Exception in thread "main" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.AggregateNode
\-[AGGREGATE] AggregateNode: 'max'
\-[ALIAS_REF] IdentNode: 'price' {alias=price, no from element}

at org.hibernate.hql.ast.SelectClause.initializeExplicitSelectClause(SelectClause.java:122)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:414)
......................................................
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:773)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at FromClauseEx.main(FromClauseEx.java:21)



The class containing hql query--

public class FromClauseEx {
public static void main(String[] s){
Session sess=null;
SessionFactory sessFact = new Configuration().configure().
buildSessionFactory();
sess= sessFact.openSession();
String sql_query = "select max(price) from Product products";
Query query = ses.createQuery(sql_query);
List list = query.list();
System.out.println("max investment amount --" +list.get(0));
}
}




The Product class----

public class Product{
private int productId;
private String proName;
private int price;
public void setProductId(int productId)
{
this.productId = productId;
}
public int getProductId()
{
return productId;
}
public void setProName(String proName)
{
this.proName = proName;
}
public String getProName()
{
return proName;
}
public void setPrice(int price)
{
this.price = price;
}
public int getPrice()
{
return price;
}
}


The Product.hbm.xml---


<hibernate-mapping>
<class name="Product" table="products">

<id name="productId" column="pid" />
<property name="proName" column="pname" length="10"/>
<property name="price" column="price" />

</class>
</hibernate-mapping>



And the table name is "products"
it contains column pid,pname,price

please help me to solve this problem
Thanks
+Pie Number of slices to send: Send
That sounds like the query isn't finding any Products, so it's trying to find the maximum of no values, which is invalid. Are you sure there are Products in the database? Try running a query to count the Products instead of find the maximum price, to check.
+Pie Number of slices to send: Send
Hi I have change the query from String sql_query = "select max(price) from Product products"; to String sql_query = "select count(*) from Product products";
it find 4 rows

output --------------

Hibernate: select count(*) as col_0_0_ from products product0_
max investment amount --

4



The products table conains 4 rows

+------+-------+-------+
| pid | pname | price |
+------+-------+-------+
| 104 | HDD | 5000 |
| 106 | Mouse | 500 |
| 101 | HDD | 4000 |
| 102 | HDD | 4500 |
+------+-------+-------+
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 940 times.
Similar Threads
Application logic & servlets
shopping cart problem
Problem with Hibernate test application
Null Pointer exception thrown while deploying the cmp bean
How to return class instance?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:59:47.