我的 HQL 查询出了什么问题?
我的 HQL 查询有问题,但找不到它。也许这里有人可以帮助我。我搜索了整个论坛和谷歌,但找不到我的问题的好答案。
这是我的类 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>
我使用此代码在表中选择 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();
当我运行此代码时,我收到 NullPointerException。当我在 Netbeans 中运行此查询时,出现错误。
查询:`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)
我的查询做错了什么?
顺便说一句,FROM Size as size
非常适合检索数据库中 Size 的所有对象。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除 HQL 中变量周围的引号 - 它是一个整数(不是字符串):
Remove the quotes from around your variable in the HQL - it's an Integer (not a String):
如果是整数,则无需添加引号。
试试这个方法
In case of Integer you don't need to add quotes.
Try this way
尽量不要使用别名
size
,因为它是 HQL 中的一个保留字,用于获取集合的大小(请参阅 Hibernate 文档)。尝试使用查询参数,它们将帮助您解决未来的很多问题。在返回之前还要检查
tshirtSize
的可为空性。对于您的示例,这将是一个很好的方法:希望有帮助
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:Hope it helps
试试这个:
WHERE size.id = " + s
try this :
WHERE size.id = " + s