Link to home
Start Free TrialLog in
Avatar of TheFACTORY_Max
TheFACTORY_MaxFlag for South Africa

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().find("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().find("select startTimeHours, startTimeMinutes from BookException")

The given error is:
SEVERE: Servlet.service() for servlet action threw exception
org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [com.GolfOnline.Beans.GolferType@7b1d52] [select startTimeHours, startTimeMinutes from BookException where bookExceptionGolferTypes = ?]

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;   }
    
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);

Open in new window

objects is right, try the following

Query query = getSession().createQuery(
             "select startTimeHours, startTimeMinutes from BookException " + 
             "join bookExceptionGolferTypes " + 
             "where bookExceptionGolferTypes.id = 3");

Open in new window

Avatar of TheFACTORY_Max

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.ServletException: org.hibernate.QueryException: illegal attempt to dereference collection [{synthetic-alias}{non-qualified-property-ref}bookExceptionGolferTypes] with element property reference [id] [select startTimeHours, startTimeMinutes from com.GolfOnline.Beans.BookException join bookExceptionGolferTypes where bookExceptionGolferTypes.id = 3]
      org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
Sorry, I forgot the alias


Query query = getSession().createQuery(
             "select startTimeHours, startTimeMinutes from BookException " +
             "join bookExceptionGolferTypes gt " +
             "where gt.id = 3");

Open in new window

Thanks, but it gives the following error now:

javax.servlet.ServletException: org.hibernate.QueryException: Unable to resolve path [gt.id], unexpected token [gt] [select startTimeHours, startTimeMinutes from com.GolfOnline.Beans.BookException join bookExceptionGolferTypes gt where gt.id = 3]
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

Query query = getSession().createQuery(
             "select startTimeHours, startTimeMinutes from BookException as be " +
             "join be.bookExceptionGolferTypes as gt " +
             "where gt.id = 3");

Open in new window

Strange error this time:

javax.servlet.ServletException: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode
 +-[IDENT] IdentNode: 'startTimeHours' {originalText=startTimeHours}
Damn! As I've introduced an alias it must be used:
be.startTimeHours

Query query = getSession().createQuery(
             "select be.startTimeHours, be.startTimeMinutes from BookException as be " +
             "join be.bookExceptionGolferTypes as gt " +
             "where gt.id = 3");

Open in new window

We'll get there, this time it this error (from the stacktrace):

Hibernate: select bookexcept0_.be_StartTimeHours as col_0_0_, bookexcept0_.be_StartTimeMinutes as col_1_0_ from tbl_BookException bookexcept0_ inner join tbl_BookExceptionGolferType bookexcept1_ on bookexcept0_.be_ID=bookexcept1_.eg_BookException_be_ID where bookexcept1_.be_ID=3
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1054, SQLState: 42S22
10-Feb-2009 15:15:02 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Unknown column 'bookexcept1_.be_ID' in 'where clause'
10-Feb-2009 15:15:02 org.apache.struts.action.RequestProcessor processException
WARNING: Unhandled Exception thrown: class org.hibernate.exception.SQLGrammarException
SOLUTION
Avatar of mahome
mahome
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It now says something different:

org.hibernate.QueryException: could not resolve property: golferType.id of: com.GolfOnline.Beans.BookException [select be.startTimeHours, be.startTimeMinutes from com.GolfOnline.Beans.BookException as be join be.bookExceptionGolferTypes as begt where begt.golferType.id = 3]
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TheFACTORY_Max, thanks for posting the solution. I'm glad you finally found a solution.