带有多个SELECT NEW语句的jpa构造函数表达式 [英] jpa constructor expressions with multiple SELECT NEW statements

查看:142
本文介绍了带有多个SELECT NEW语句的jpa构造函数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在 jpql 查询(Hibernate)中有多个 SELECT NEW 语句的方法?



这适用于我:

  @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();

我在 DTO

  @Component 
public class ItemServiceDTO {

private item item;
private service serviceType;
私人服务serviceLocation;
私人服务serviceRegion;

public ItemServiceDTO(item item,Service serviceType,Service serviceLocation,Service serviceRegion){
super();
this.item = item;
this.serviceType = serviceType;
this.serviceLocation = serviceLocation;
this.serviceRegion = serviceRegion;
}

但我想要的是有一个新的语言及其构造器。



例如:

  @Query(SELECT NEW com.test.project.dto.ItemService(g,s,l,r),new LanguageDTO()
+FROM Item g,Service s,Service l,Service r

或者在 ItemService

的子选择中

  @Query(SELECT NEW com.test.project.dto.ItemService(g,s,l,r,new LanguageDTO())
+ FROM Item g,Service s,Service l,Service r

我也有兴趣使用 Map 列表在我的DTO对象中,但我读过那些是不可能的?是吗?



在使用这两个示例时,我的Spring引导应用程序确实以错误开始。



最后,我想要一个Map of Map< List< Item>,Map< List< LanguageDTO>,List< ItemServiceDTO>>> map;

解决方案

从技术上讲,根据JPQL select子句的定义,它具有w应该允许多个构造函数表达式。

lockquote


  • select_clause :: = SELECT [DISTINCT] select_expression {,select_expression} *

  • select_expression :: = single_valued_pa​​th_expression |聚合表达式| identification_variable |

    OBJECT(identification_variable)| constructor_expression

  • constructor_expression :: = NEW构造函数名(constructor_item {,constructor_item} *) 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 

然而,我只是发现Hibernate不允许它。当我将JPA提供程序从Hibernate切换到EclipseLink时,它可以工作。因此,如果允许使用这种查询语法,您可能需要咨询您的提供者。



但是,请注意,使用NEW运算符时,构造函数必须具有参数至少有一个)。所以这个表达式不起作用:

  SELECT NEW LanguageDTO()

在第二个问题中,是否可以使用 List Map ,我很困惑如何在查询中使用这些集合。但是,请注意,按照JPQL SELECT_CLAUSE的定义,在SELECT子句中不可能有集合值路径表达式。


Is there a way to have multiple SELECT NEW statements in a jpql query (Hibernate)?

This works for me:

@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();

I get the expected Result in my 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;
    }

But what I want is to have a new instance of Language with its contructor.

For example like this:

 @Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r), new LanguageDTO()"
            +" FROM Item g, Service s, Service l , Service r"

Or in a subselect of ItemService

 @Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r, new LanguageDTO())"
                +" FROM Item g, Service s, Service l , Service r"

I also interested in using Map and List in my DTO Objects but I read thats not possible? Is that right?

My Spring boot application does start with errors while using the two examples.

At the end I want a Map of Map<List<Item>,Map<List<LanguageDTO>,List<ItemServiceDTO>>> map;

解决方案

Technically, by definition of JPQL select clause, it would allow multiple constructor expressions.

  • 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)

Example:

SELECT NEW com.test.model.UserName(u.firstname, u.lastname), NEW com.test.model.UserEmail(u.email) FROM User u

However, I just discovered that Hibernate does not allow it. When I switched JPA provider from Hibernate to EclipseLink, it works. So, you may need to consult with your provider if such query syntax is allowed.

Take note however, that when using NEW operator, your constructor has to have arguments (at least one). So this expression won't work:

SELECT NEW LanguageDTO()

On your second question, whether it is possible to use List and Map, I'm quite confused how you would want to use these collections in your query. However, take note that it is not possible to have collection valued path expressions in your SELECT clause as per definition of the JPQL SELECT_CLAUSE.

这篇关于带有多个SELECT NEW语句的jpa构造函数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆