• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

max function showing error

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 |
+------+-------+-------+
 
reply
    Bookmark Topic Watch Topic
  • New Topic