-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Aggregate functions (max)
PostPosted: Tue Jun 17, 2008 12:21 am 
Beginner
Beginner

Joined: Tue Jun 17, 2008 12:14 am
Posts: 21
I am new to Hibernate, and I have an XML:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.flextronics.flexflow.LuJIG" table="luJIG">
      <id name="id" type="int">
            <column name="ID" />
        </id>
        <property name="description" type="string">
            <column name="Description" length="200" />
        </property>
  </class>
</hibernate-mapping>


why I do something like:

Code:
result = session.createQuery("Select max(lj.id) from LuJIG lj")
.setString(0, strFixture)
.list();
if (result.size() > 0) { 
                   
}



will hit erorr:

Quote:
java.lang.IllegalArgumentException: No positional parameters in query: Select max(lj.id) from LuJIG lj
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:332)
at org.hibernate.impl.AbstractQueryImpl.setString(AbstractQueryImpl.java:457)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 4:04 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This is typically used to set the value for a placeholder:

setString(0, strFixture)

I can't see the 0th element you are setting?

A typical use of variable injection might be something like this:



Code:
Session session = HibernateUtil.beginTransaction();
String loginName = "mj";
String hql="from User where loginName = :name";
Query query = session.createQuery(hql);
query.setString("name", loginName);
Object o = query.uniqueResult();
User user = (User)o;
System.out.println(user.getLoginName());
HibernateUtil.commitTransaction();


Take a look here for some simple Hibernate3 tutorials and information on how to use HQL variable injection queries, if that's what you need:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 4:08 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This is typically used to set the value for a placeholder:

setString(0, strFixture)

I can't see the 0th element you are setting?

A typical use of variable injection might be something like this:



Code:
Session session = HibernateUtil.beginTransaction();
String loginName = "mj";
String hql="from User where loginName = :name";
Query query = session.createQuery(hql);
query.setString("name", loginName);
Object o = query.uniqueResult();
User user = (User)o;
System.out.println(user.getLoginName());
HibernateUtil.commitTransaction();


Take a look here for some simple Hibernate3 tutorials and information on how to use HQL variable injection queries, if that's what you need:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 10:12 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
What is that 0th???

result = session.createQuery("Select max(lj.id) as luJIGid from LuJIG lj")
.list();

or
List results = session.createCriteria(LuJIG.class)
.setProjection( Projections.projectionList()
.add( Projections.max("id") )
)
.list();

Hope this helps:-) Else post more info on the 0th???

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 11:10 am 
Beginner
Beginner

Joined: Tue Jun 17, 2008 12:14 am
Posts: 21
Thanks ramnath & Cameron McKenzie. I think there's no param at 0 th position. I just totally new to hsql, especially on aggregate. I will try.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 9:12 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
0th is the element between -1th and 1th.

:)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.