Pat*_*ick 11 java spring hibernate jpa jpql
有没有办法SELECT NEW在jpql查询中使用多个语句(Hibernate)?
这对我有用:
@Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r) "
+" FROM Item g, Service s, Service l , Service r"
+" WHERE s.id = g.id"
+" AND s.location = l.name"
+" AND s.serviceType = 'type'"
+" AND l.serviceType = 'Location'"
+" AND l.area = r.name"
+" AND r.serviceType = 'Region'")
public List<Item> getAllItemsWithServices();
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果DTO.
@Component
public class ItemServiceDTO{
private Item item;
private Service serviceType;
private Service serviceLocation;
private Service serviceRegion;
public ItemServiceDTO(item item, Service serviceType, Service serviceLocation, Service serviceRegion) {
super();
this.item = item;
this.serviceType = serviceType;
this.serviceLocation = serviceLocation;
this.serviceRegion = serviceRegion;
}
Run Code Online (Sandbox Code Playgroud)
但我想要的是拥有一个Language带有它的构造函数的新实例.
例如这样:
@Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r), new LanguageDTO()"
+" FROM Item g, Service s, Service l , Service r"
Run Code Online (Sandbox Code Playgroud)
或者在子选择中 ItemService
@Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r, new LanguageDTO())"
+" FROM Item g, Service s, Service l , Service r"
Run Code Online (Sandbox Code Playgroud)
我也有兴趣在我的DTO对象中使用Map,List但我读过那些不可能的东西?是对的吗?
使用这两个示例时,我的Spring启动应用程序确实以错误开始.
最后我想要一张地图 Map<List<Item>,Map<List<LanguageDTO>,List<ItemServiceDTO>>> map;
Ish*_*Ish 14
从技术上讲,根据JPQL select子句的定义,它将允许多个构造函数表达式.
- select_clause :: = SELECT [DISTINCT] select_expression {,select_expression}*
- select_expression :: = single_valued_path_expression | aggregate_expression | identification_variable |
OBJECT(identification_variable)| constructor_expression- constructor_expression :: = NEW constructor_name(constructor_item {,constructor_item}*)
- constructor_item :: = single_valued_path_expression | aggregate_expression
- aggregate_expression :: = {AVG | MAX | MIN | SUM}([DISTINCT] state_field_path_expression)| COUNT([DISTINCT]
identification_variable | state_field_path_expression |
single_valued_association_path_expression)
例:
SELECT NEW com.test.model.UserName(u.firstname, u.lastname), NEW com.test.model.UserEmail(u.email) FROM User u
Run Code Online (Sandbox Code Playgroud)
但是,我刚刚发现Hibernate不允许它.当我将JPA提供程序从Hibernate切换到EclipseLink时,它可以工作.因此,如果允许此类查询语法,您可能需要咨询您的提供商.
但请注意,使用NEW运算符时,构造函数必须包含参数(至少一个).所以这个表达式不起作用:
SELECT NEW LanguageDTO()
Run Code Online (Sandbox Code Playgroud)
关于你的第二个问题,是否可以使用List和Map,我很困惑你想如何在查询中使用这些集合.但是,请注意,根据JPQL SELECT_CLAUSE的定义,不可能在SELECT子句中具有集合值路径表达式.
| 归档时间: |
|
| 查看次数: |
11010 次 |
| 最近记录: |