Is this possible: JPA/Hibernate query with list property in result?

前端 未结 3 591
无人及你
无人及你 2020-12-14 10:51

In hibernate I want to run this JPQL / HQL query:

select new org.test.userDTO( u.id, u.name, u.securityRoles)
FROM User u
WHERE u.name = :na         


        
相关标签:
3条回答
  • 2020-12-14 11:13

    I believe you need to declare a 0-arg constructor in your UserDTO class.

    EDIT: Or a constructor which takes Integer instead of int as the first argument. When looking up constructors using reflection Hibernate might not treat them as 'compatible' types.

    Basically, I would focus on the Unable to locate appropriate constructor on class [...UserDTO] part of the message.

    0 讨论(0)
  • I think that you should try something like:

    select new org.test.userDTO( u.id, u.name, u.securityRoles) AS uDTO,
      uDTO.setRoles(u.securityRoles)
     FROM User u
     WHERE u.name = :name
    
    0 讨论(0)
  • 2020-12-14 11:21

    The query without the NEW (selecting a scalar value and a collection-valued path expression) isn't valid so I don't think that adding a NEW will make things work.

    For the record, this is what the JPA 2.0 specification says in the section 4.8 SELECT Clause:

    The SELECT clause has the following syntax:

    select_clause ::= SELECT [DISTINCT] select_item {, select_item}*
    select_item ::= select_expression [ [AS] result_variable]
    select_expression ::=
             single_valued_path_expression |
             scalar_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 |
             scalar_expression |
             aggregate_expression |
             identification_variable
    aggregate_expression ::=
             { AVG | MAX | MIN | SUM } ([DISTINCT] state_field_path_expression) |
             COUNT ([DISTINCT] identification_variable | state_field_path_expression |
                      single_valued_object_path_expression)
    
    0 讨论(0)
提交回复
热议问题