TheFACTORY_Max
asked on
Hibernate hql query using list collection mapping problem
Hello everyone,
What I am trying to achieve is to use a HQL query to select objects, the problems start when I want to set a where condition which uses a List collection map.
the used hibernate hbm and java beans are in the code section.
The current (not working) query goes:
golfertype = new GolferType();
golfertype.setId((short)3) ;
getHibernateTemplate().fin d("select startTimeHours, startTimeMinutes from BookException where bookExceptionGolferTypes = ?", golfertype)
This below does work (so it is caused by the collection mapping used in the where query):
getHibernateTemplate().fin d("select startTimeHours, startTimeMinutes from BookException")
The given error is:
SEVERE: Servlet.service() for servlet action threw exception
org.hibernate.QueryExcepti on: Expected positional parameter count: 1, actual parameters: [com.GolfOnline.Beans.Golf erType@7b1 d52] [select startTimeHours, startTimeMinutes from BookException where bookExceptionGolferTypes = ?]
I have no idea how to do this..
Thanks, Max
What I am trying to achieve is to use a HQL query to select objects, the problems start when I want to set a where condition which uses a List collection map.
the used hibernate hbm and java beans are in the code section.
The current (not working) query goes:
golfertype = new GolferType();
golfertype.setId((short)3)
getHibernateTemplate().fin
This below does work (so it is caused by the collection mapping used in the where query):
getHibernateTemplate().fin
The given error is:
SEVERE: Servlet.service() for servlet action threw exception
org.hibernate.QueryExcepti
I have no idea how to do this..
Thanks, Max
The following relevant hibernate hbm's are being used:
----------------------------------------- BookException.hbm
<hibernate-mapping>
<class name="com.GolfOnline.Beans.BookException" table="tbl_BookException" lazy="false">
<id name="id" column="be_ID" type="short">
<generator class="native"/>
</id>
<list name="bookExceptionGolferTypes" table="tbl_BookExceptionGolferType" lazy="false" inverse="true">
<key column="eg_BookException_be_ID"/>
<list-index base="0" column="eg_ID"/>
<composite-element class="com.GolfOnline.Beans.BookExceptionGolferType">
<many-to-one class="com.GolfOnline.Beans.GolferType" column="eg_GolferType_gt_ID" name="golferType"/>
<property name="fee" column="eg_Fee" type="integer" />
<property name="id" column="eg_Id" type="integer" />
</composite-element>
</list>
<property name="startTimeHours" column="be_StartTimeHours"/>
<property name="startTimeMinutes" column="be_StartTimeMinutes"/>
</class>
</hibernate-mapping>
----------------------------------------- BookExceptionGolferType.hbm
<hibernate-mapping>
<class name="com.GolfOnline.Beans.BookExceptionGolferType" table="tbl_BookExceptionGolferType" lazy="false">
<id name="id" column="eg_ID">
<generator class="native"/>
</id>
<many-to-one name="bookException" column="eg_BookException_be_ID" class="com.GolfOnline.Beans.BookException"/>
<many-to-one name="golferType" column="eg_GolferType_gt_ID" class="com.GolfOnline.Beans.GolferType"/>
<property name="fee" column="eg_Fee"/>
</class>
</hibernate-mapping>
----------------------------------------- GolferType.hbm
<hibernate-mapping>
<class name="com.GolfOnline.Beans.GolferType" table="tbl_GolferType" lazy="false">
<id name="id" column="gt_ID" type="short">
<generator class="native"/>
</id>
<property name="title" column="gt_Title"/>
</class>
</hibernate-mapping>
The following relevant java beans is being used:
----------------------------------------- BookException .java
public class BookException implements Serializable {
private List bookexceptiongolfertypes = null;
private short id;
private String starttimehours;
private String starttimeminutes;
public BookException() {
super();
}
public List getBookExceptionGolferTypes() { return bookexceptiongolfertypes; }
public short getId() { return id; }
public String getStartTimeHours() { return starttimehours; }
public String getStartTimeMinutes() { return starttimeminutes; }
public void setBookExceptionGolferTypes(List bookexceptiongolfertypes) { this.bookexceptiongolfertypes = bookexceptiongolfertypes; }
public void setId (short id) { this.id = id; }
public void setStartTimeHours (String starttimehours) { this.starttimehours = starttimehours; }
public void setStartTimeMinutes (String starttimeminutes) { this.starttimeminutes = starttimeminutes; }
}
you're comparing a list to an instance by the looks,don't you mean .... contains ?
You have to set an entity instead of a parameter:
Query query = getSession().createQuery(("select startTimeHours, startTimeMinutes from BookException where bookExceptionGolferTypes = :golferType");
query.setEntity("golferType", golferType);
objects is right, try the following
Query query = getSession().createQuery(
"select startTimeHours, startTimeMinutes from BookException " +
"join bookExceptionGolferTypes " +
"where bookExceptionGolferTypes.id = 3");
ASKER
Objects:
Yes it should only return those which contain the given argument in the collection.
mahome:
Your last suggestion gives the following error:
javax.servlet.ServletExcep tion: org.hibernate.QueryExcepti on: illegal attempt to dereference collection [{synthetic-alias}{non-qua lified-pro perty-ref} bookExcept ionGolferT ypes] with element property reference [id] [select startTimeHours, startTimeMinutes from com.GolfOnline.Beans.BookE xception join bookExceptionGolferTypes where bookExceptionGolferTypes.i d = 3]
org.apache.struts.action.R equestProc essor.proc essExcepti on(Request Processor. java:535)
Yes it should only return those which contain the given argument in the collection.
mahome:
Your last suggestion gives the following error:
javax.servlet.ServletExcep
org.apache.struts.action.R
Sorry, I forgot the alias
Query query = getSession().createQuery(
"select startTimeHours, startTimeMinutes from BookException " +
"join bookExceptionGolferTypes gt " +
"where gt.id = 3");
ASKER
Thanks, but it gives the following error now:
javax.servlet.ServletExcep tion: org.hibernate.QueryExcepti on: Unable to resolve path [gt.id], unexpected token [gt] [select startTimeHours, startTimeMinutes from com.GolfOnline.Beans.BookE xception join bookExceptionGolferTypes gt where gt.id = 3]
javax.servlet.ServletExcep
Sorry again, try the following: (Old problem of me, I always need iterations to the correct code)
Have a look here for hql: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql
Have a look here for hql: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql
Query query = getSession().createQuery(
"select startTimeHours, startTimeMinutes from BookException as be " +
"join be.bookExceptionGolferTypes as gt " +
"where gt.id = 3");
ASKER
Strange error this time:
javax.servlet.ServletExcep tion: java.lang.IllegalStateExce ption: No data type for node: org.hibernate.hql.ast.tree .IdentNode
+-[IDENT] IdentNode: 'startTimeHours' {originalText=startTimeHou rs}
javax.servlet.ServletExcep
+-[IDENT] IdentNode: 'startTimeHours' {originalText=startTimeHou
Damn! As I've introduced an alias it must be used:
be.startTimeHours
be.startTimeHours
Query query = getSession().createQuery(
"select be.startTimeHours, be.startTimeMinutes from BookException as be " +
"join be.bookExceptionGolferTypes as gt " +
"where gt.id = 3");
ASKER
We'll get there, this time it this error (from the stacktrace):
Hibernate: select bookexcept0_.be_StartTimeH ours as col_0_0_, bookexcept0_.be_StartTimeM inutes as col_1_0_ from tbl_BookException bookexcept0_ inner join tbl_BookExceptionGolferTyp e bookexcept1_ on bookexcept0_.be_ID=bookexc ept1_.eg_B ookExcepti on_be_ID where bookexcept1_.be_ID=3
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExc eptionRepo rter logExceptions
WARNING: SQL Error: 1054, SQLState: 42S22
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExc eptionRepo rter logExceptions
SEVERE: Unknown column 'bookexcept1_.be_ID' in 'where clause'
10-Feb-2009 15:15:02 org.apache.struts.action.R equestProc essor processException
WARNING: Unhandled Exception thrown: class org.hibernate.exception.SQ LGrammarEx ception
Hibernate: select bookexcept0_.be_StartTimeH
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExc
WARNING: SQL Error: 1054, SQLState: 42S22
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExc
SEVERE: Unknown column 'bookexcept1_.be_ID' in 'where clause'
10-Feb-2009 15:15:02 org.apache.struts.action.R
WARNING: Unhandled Exception thrown: class org.hibernate.exception.SQ
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It now says something different:
org.hibernate.QueryExcepti on: could not resolve property: golferType.id of: com.GolfOnline.Beans.BookE xception [select be.startTimeHours, be.startTimeMinutes from com.GolfOnline.Beans.BookE xception as be join be.bookExceptionGolferType s as begt where begt.golferType.id = 3]
org.hibernate.QueryExcepti
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
TheFACTORY_Max, thanks for posting the solution. I'm glad you finally found a solution.