简体   繁体   English

我的HQL查询有什么问题?

[英]What is wrong with my HQL Query?

I have a problem with my HQL query and I can't find it. 我的HQL查询有问题,但找不到。 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. 我已经搜索了整个论坛并在Google上进行了搜索,但是找不到对我的问题的好的答案。

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. 我使用此代码在表中选择id = 1。

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. 当我运行此代码时,我得到一个NullPointerException。 And when I run this query in Netbeans, I get an Error. 当我在Netbeans中运行此查询时,出现错误。

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

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. 顺便说一下, FROM Size as size可以完美地检索数据库中所有Size的对象。

删除HQL中变量周围的引号-这是一个整数(不是字符串):

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

In case of Integer you don't need to add quotes. 如果是Integer,则无需添加引号。

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

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 ). 尽量不要使用别名size ,因为它是HQL中的保留字 ,用于获取集合的大小(请参见Hibernate Docs中的内容 )。

Try using query parameters , they will help you with a lot of future problems. 尝试使用查询参数 ,它们将帮助您解决许多将来的问题。 Also check nullability of tshirtSize before returning it. 还要在返回之前检查tshirtSize空性。 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 希望能帮助到你

试试这个: WHERE size.id = " + s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM