java 带有多个 SELECT NEW 语句的 jpa 构造函数表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33396847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:36:40  来源:igfitidea点击:

jpa constructor expressions with multiple SELECT NEW statements

javaspringhibernatejpajpql

提问by Patrick

Is there a way to have multiple SELECT NEWstatements in a jpqlquery (Hibernate)?

有没有办法SELECT NEWjpql查询(休眠)中有多个语句?

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.

我在我的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 Languagewith its contructor.

但我想要的是拥有一个Language带有其构造函数的新实例。

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

或者在一个子选择中 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 Mapand Listin my DTO Objects but I read thats not possible? Is that right?

我也对在我的 DTO 对象中使用Map和感兴趣,List但我读到那是不可能的?是对的吗?

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

我的 Spring boot 应用程序在使用这两个示例时确实以错误开头。

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

最后我想要一张地图 Map<List<Item>,Map<List<LanguageDTO>,List<ItemServiceDTO>>> map;

回答by Ish

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

从技术上讲,根据 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_clause ::= SELECT [DISTINCT] select_expression {, select_expression}*
  • select_expression ::= single_valued_pa​​th_expression | 聚合表达式 | 识别变量|
    对象(标识_变量)| 构造函数表达式
  • 构造函数表达式 ::= 新构造函数名称(构造函数项{,构造函数项}*)
  • 构造函数 ::= single_valued_pa​​th_expression | 聚合表达式
  • 聚合表达式 ::= { AVG | 最大 | 最小 | SUM } ([DISTINCT] state_field_path_expression) | 计数([DISTINCT]
    标识变量 |
    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.

但是,我刚刚发现 Hibernate 不允许这样做。当我将 JPA 提供程序从 Hibernate 切换到 EclipseLink 时,它可以工作。因此,如果允许此类查询语法,您可能需要咨询您的提供商。

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

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

SELECT NEW LanguageDTO()

On your second question, whether it is possible to use Listand 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.

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