在hql查询中选择多个new()对象 [英] select multiple new() object in hql query
问题描述
我对hql语言有疑问.我正在尝试通过WCF REST应用程序上的hql语法生成DTO.我对第二个查询有疑问.怎么了还有其他方法可以达到相同的结果吗?
I have a question about hql language. I'm trying to generate DTO's via hql syntax on my WCF REST application. I have a problem with the second query. What is wrong with it? Is there any other way to reach the same result?
这很好.
session.CreateQuery(@"select new EntityTypeDTO(t.ID, t.Title, assc.ID)
from crmEntityType t
left outer join t.Association as assc").List<EntityTypeDTO>();
这不起作用.
session.CreateQuery(@"select new EntityTypeDTO(t.ID, t.Title, assc.ID, new CustomFieldDTO(f.ID,f.EntityType,f.FieldType,f.Name,f.Value))
from crmEntityType t
join fetch t.Fields as f
left outer join t.Association as assc").List<EntityTypeDTO>();
推荐答案
好吧,new Xxx(field1, field2, ...)
语法只是在查询中创建这些DTO的便捷方式.它不是一种完全成熟的编程语言,因此,它很可能不支持嵌套的new
调用,如您的new EntityTypeDTO(..., new CustomFieldDTO(...))
.
Well, the new Xxx(field1, field2, ...)
syntax is just a convenience way for creating those DTOs in a query. It is not a fully fledged programming language and as such it most probably won't support nested new
calls like your new EntityTypeDTO(..., new CustomFieldDTO(...))
.
您可以做的是在Object[]
中选择字段并自己调用构造函数,例如像这样:
What you could do instead is select the fields into a Object[]
and call the constructors yourself, e.g. like this:
List<Object[]> result = query.getResultList();
for( Object[] line : result ) {
//this is just an example, the order depends on the field order in the select clause
//and you'd need some casts/conversion
new EntityTypeDTO(line[0], line[1], line[2], new CustomFieldDTO(line[3], ...));
}
这篇关于在hql查询中选择多个new()对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!