HQL:如何选择不同列的所有实体? [英] HQL: How to select all entities distinct by some column?
本文介绍了HQL:如何选择不同列的所有实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个简单的问题:
在这个例子中,我需要检索所有对象,但这些对象必须有不同的msgFrom字段。
当我使用
List< Message> list = getHibernateTemplate()。find(从消息m中选择不同的m.msgFrom WHERE msgTo =?AND msgCheck = 0,dinc);
我得到下一个错误:
java.lang.ClassCastException:java.lang.Integer不能转换为com.example.model.Message
我想这是因为Hibernate只检索一列,但我需要一个对象,而不是列。
我该怎么做?
我认为我可以通过逗号滚动,即
List< Message> list = getHibernateTemplate()。find(select m.msgFrom,m.To,m.datetime,.......... from Message m WHERE msgTo =?AND msgCheck = 0,dinc);
但是如果我在这里有20多个字段呢?
谢谢!
解决方案<下面是示例查询:
select e from Message e
where e.msgFrom IN (从消息m
中选择不同的m.msgFrom
WHERE m.msgTo =?
AND m.msgCheck =0);
或者,您也可以使用Criteria API。
A simple question:
In this example I need to retrieve all objects, but these objects must have distinct msgFrom fields.
When I use
List<Message> list = getHibernateTemplate().find("select distinct m.msgFrom from Message m WHERE msgTo = ? AND msgCheck = 0", dinc);
I get next error:
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.model.Message
I suppose it's because Hibernate retrieves only one column, but I need an object, not column.
How can I do this?
I think that I can just scroll through a comma, i.e.
List<Message> list = getHibernateTemplate().find("select distinct m.msgFrom, m.To, m.datetime, .......... from Message m WHERE msgTo = ? AND msgCheck = 0", dinc);
But what if I have more than 20 fields here? Is there an easy solution?
Thanks!
解决方案
Below is the sample query :
select e from Message e
where e.msgFrom IN (select distinct m.msgFrom
from Message m
WHERE m.msgTo = ?
AND m.msgCheck = "0");
Alternatively, you can also use Criteria API.
这篇关于HQL:如何选择不同列的所有实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文