使用 HibernateTemplate (Spring) 的 HQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10131146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HQL queries using HibernateTemplate (Spring)
提问by n00bc0der
This is my first post so please bear with me. I'm trying to execute a HQL query from within a method using HibernateTemplate. The reason I'm doing this way is because the actual table has many more columns but I'm only interested in only two. The result of the query is used in performing updateOrSave operation later.
这是我的第一篇文章,所以请耐心等待。我正在尝试从使用 HibernateTemplate 的方法中执行 HQL 查询。我这样做的原因是因为实际表有更多的列,但我只对两个感兴趣。查询结果用于稍后执行 updateOrSave 操作。
So the class structure looks like:
所以类结构看起来像:
class XYZDAO {
...
...
public void createReview(....) {
...
...
class Temp {
private final float rating;
private final int count;
@SuppressWarnings("unused")
public Temp(float rating, int count)
{
this.rating = rating;
this.count = count;
}
public float getRating()
{
return rating;
}
public int getCount()
{
return count;
}
}
List<Temp> avgRatingWrapper = getHibernateTemplate().find("SELECT new Temp(AVG(RATING), COUNT(*)) FROM RATINGS WHERE ADVENTURE_ID = ?", Integer.parseInt(adventureId));
...
...
}
}
When I run the code following exception occurs:
当我运行代码时发生以下异常:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: RATINGS is not mapped [SELECT new Temp(AVG(RATING), COUNT(*)) FROM RATINGS WHERE ADVENTURE_ID = ?]
I already have a full blown mapping hibernate mapping for the RATINGS table:
我已经为 RATINGS 表创建了一个完整的映射休眠映射:
<hibernate-mapping>
<class name="com.xyz.abc.dao.hibernate.Ratings" table="ADV_ADMN.RATINGS">
<id name="id" type="int" column="ID">
<generator class="seqhilo">
<param name="sequence">ADV_ADMN.RATINGS_ID_SEQ</param>
<param name="allocationSize">1</param>
</generator>
</id>
<version name="timestamp" type="timestamp">
<column name="TIMESTAMP" length="19" not-null="true" />
</version>
<property name="adventureId" type="int">
<column name="ADVENTURE_ID" not-null="true" />
</property>
<property name="reviewer" type="string">
<column name="REVIEWER" length="45" not-null="true" />
</property>
<property name="rating" type="java.lang.Float">
<column name="RATING" not-null="true" />
</property>
</class>
</hibernate-mapping>
Now i understand that I need to perform some kind of mapping either in the hibernate.hbm.xml files or provide annotation for the Temp class to map the RATINGS table. I was wondering if there is any other way to get around the problem. I figured if you use Session.createSqlQuery(....) then you can add entities to it which can circumvent the problem. But I'm not sure is there is a way to do that in HibernateTemplate.
现在我明白我需要在 hibernate.hbm.xml 文件中执行某种映射,或者为 Temp 类提供注释以映射 RATINGS 表。我想知道是否有其他方法可以解决这个问题。我想如果你使用 Session.createSqlQuery(....) 那么你可以向它添加实体来规避这个问题。但我不确定在 HibernateTemplate 中是否有办法做到这一点。
Any help/pointers are greatly appreciated.
非常感谢任何帮助/指示。
回答by axtavt
HibernateTemplate.find()doesn't take SQL query, in takes HQL query. As long as you can express your query in HQL in would be better to do so.
HibernateTemplate.find()不采用 SQL 查询,采用 HQL 查询。只要你能在 HQL 中表达你的查询就更好了。
Otherwise you need to use Session.createSqlQuery()inside a callback passed to HibernateTemplate.executeFind().
否则,您需要Session.createSqlQuery()在传递给HibernateTemplate.executeFind().
See also:
也可以看看:
回答by Jordi
Code it's not tested, but I think you mean something like this. Create the SQLQUery, and define the scalars as Strings, and use the getHibernateTemplate().execute() function.
代码未经测试,但我认为您的意思是这样的。创建 SQLQUery,并将标量定义为字符串,并使用 getHibernateTemplate().execute() 函数。
return getHibernateTemplate().execute(new HibernateCallback<List>() {
public String doInHibernate(Session s)
throws HibernateException, SQLException {
SQLQuery sql=s.createSQLQuery("SELECT AVG(RATING) as r, COUNT(*) as c FROM RATINGS WHERE ADVENTURE_ID = ?");
sql.setParameter(0, adventureId);
sql.addScalar("r");
sql.addScalar("c");
return sql.list();
}
});
If it's HQL and getHibernateTemplate().find what you want to use, then use "select ... from com.xyz.abc.dao.hibernate.Ratings"
如果它是 HQL 和 getHibernateTemplate().find 你想要使用的,然后使用“select ... from com.xyz.abc.dao.hibernate.Ratings”

