Digihash
Digihash

Reputation: 375

What is wrong with my HQL Query?

I have a problem with my HQL query and I can't find it. Maybe someone here can help me. I've searched the whole forum and on Google but can't find a good answer to my question.

This is the mapping of my class Size:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 1, 2011 3:07:44 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="be.digihash.java.servlets.database.Size" table="Size" catalog="wielsbeeksebcdb">
        <id name="idSize" type="java.lang.Integer">
            <column name="idSize" />
            <generator class="identity" />
        </id>
        <property name="tshirtSize" type="string">
            <column name="tshirtSize" length="45" not-null="true" />
        </property>
        <bag lazy="true" cascade="all" name="users">
            <key column="idUser"/>
            <one-to-many class="be.digihash.java.servlets.database.User"/>
        </bag>
    </class>
</hibernate-mapping>

And I use this code to select the id=1 in my table.

List tshirtSize = null;
try {
    tx = session.beginTransaction();
    tshirtSize = session.createQuery("SELECT size.tshirtsize FROM Size as size WHERE size.idSize='" + s + "'").list();

} catch (Exception e) {
    System.out.println(e.getMessage());
}
return tshirtSize.get(0).toString();

When I run this code, I get a NullPointerException. And when I run this query in Netbeans, I get an Error.

Query: `SELECT size.tshirtsize FROM Size as size WHERE size.idSize='1'`` ERROR:

org.hibernate.QueryException: unexpected token: size.idSize.size [SELECT size.tshirtsize FROM be.digihash.java.servlets.database.Size as size WHERE size.idSize='1']
at org.hibernate.hql.classic.FromParser.token(FromParser.java:105)
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86)
at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:108)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:216)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:185)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)

What am I doing wrong with my query?

By the way, FROM Size as size works perfect to retrieve all the objects of Size in the database.

Upvotes: 1

Views: 1882

Answers (4)

subodh
subodh

Reputation: 6158

In case of Integer you don't need to add quotes.

Try this way

 String hql = "SELECT size.tshirtsize FROM Size as size WHERE size.idSize=?";
 List tshirtSize= session.createQuery(hql).setString(0,"123").list(); 
 if(tshirtSize!=null && tshirtSize.size>0)
  { 
       return tshirtSize.get(0).toString();
  }

Upvotes: 2

I.G. Pascual
I.G. Pascual

Reputation: 5965

Try not to use the alias size, as it is a reserved word in HQL used to get the size of collections (see in Hibernate Docs).

Try using query parameters, they will help you with a lot of future problems. Also check nullability of tshirtSize before returning it. For your example this would be a good approach:

List tshirtSize = null;
try {
    tx = session.beginTransaction();
    Query query = session.createQuery("SELECT s.tshirtsize FROM Size as s WHERE s.idSize = :idSize");
    query.setInteger("idSize", s);
    tshirtSize = query.list();

} catch (Exception e) {
    System.out.println(e.getMessage());
}
if (tshirtSize != null) {
    return tshirtSize.get(0).toString();
}
return null; // return new ArrayList(); would be perfectly valid

Hope it helps

Upvotes: 1

Bastiflew
Bastiflew

Reputation: 1166

try this : WHERE size.id = " + s

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

Remove the quotes from around your variable in the HQL - it's an Integer (not a String):

"... WHERE size.idSize = " + s

Upvotes: 2

Related Questions