HQL的新对象 [英] New object with HQL
问题描述
查询:
$ b在HQL查询中创建一个对象,但无法弄清楚我做错了什么。$ b
String query =从产品AS产品$ b中选择product.code,SUM(product.price),COUNT(product.code)
$ b GROUP BY product.code
(或者我应该使用新的MyCustomList(product.code,SUM (...,即使它没有被映射)
现在我想把这个返回的列表转换成一个类似的对象:
public MyCustomList {
public String code;
public BigDecimal price;
public int total;
//构造函数$ b $ public MyCustomList(String code ,String price,int total){// ...
检索数据:
//这抛出ClassCastException
List< MyCustomList> list = MyClass.find(query).fetch();
使用Play框架
我认为 15.6。 select子句包含了你想要实现的内容:
15.6。 select子句
...
查询可以返回多个对象
和/或属性作为
Object []
类型的数组:
select mother,offspr,mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
或作为
列表
:选择新列表(母亲,offspr,mate.name)
作为母亲
从内部加入mother.mate作为伴侣
离开外部加入mother.kittens作为offspr
或者 - 假设类
code>
有一个合适的构造函数 - 作为
实际类型安全的Java对象:
select新家庭(母亲,队友,offspr)
从DomesticCat作为母亲
加入mother.mate成为队友
离开加入mother.kittens as offspr
在您的情况中,您可能想要:
SELECT new MyCustomList .code,SUM(product.price),COUNT(product.code))从产品AS产品获得
GROUP BY product.code
其中
MyCustomList
不一定是映射的实体。Trying to create an object from an HQL query, but just can't figure out what i'm doing wrong.
Query:
String query = "SELECT product.code, SUM(product.price), COUNT(product.code) from Product AS product GROUP BY product.code"
(or should I use new MyCustomList(product.code, SUM(... , even though it's not mapped?) Now I want to cast this returned list into a similar object:
class MyCustomList{ public String code; public BigDecimal price; public int total; // Constructor public MyCustomList(String code, String price, int total){ //...
Retrieving the data:
// This throws ClassCastException List<MyCustomList> list = MyClass.find(query).fetch();
Using Play framework
解决方案I think that the section 15.6. The select clause covers what you're trying to achieve:
15.6. The select clause
...
Queries can return multiple objects and/or properties as an array of type
Object[]
:select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr
Or as a
List
:select new list(mother, offspr, mate.name) from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr
Or - assuming that the class
Family
has an appropriate constructor - as an actual typesafe Java object:select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr
In your case, you probably want:
SELECT new MyCustomList(product.code, SUM(product.price), COUNT(product.code)) from Product AS product GROUP BY product.code
Where
MyCustomList
is not necessarily a mapped entity.这篇关于HQL的新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!