Create Dto by the constructor with a List parameter of a Hibernate entity

4

I have a @Query Spring, which performs a query to the bank by an Entity, to create some DTO Objects. And the properties of this DTO object, I passed via Builder, but I needed to pass as a parameter on the List constructor it has in the Entity.

public OperacaoDTO(List<Staging> stagings, BigDecimal valor) {
        this.stagings = stagings;
        this.valor = valor;
    }

My @Query

@Query("SELECT NEW br.com.teste.OperacaoDTO(operacao.stagings, operacao.valor) FROM Operacao operacao")
    List<OperacaoDto> findAll();

The problem he complains about saying he does not have an appropriate constructor in OperationDTO. Is there any way for this List of the Entity to pass in the constructor? because I'm going to perform some treatment on this List

    
asked by anonymous 14.11.2014 / 20:52

1 answer

1

You should create a constructor as follows for the object

OperacaoDTO(operacao.stagings, operacao.valor)

The types must be the same as the Object, for example: if value for Double and stagings for String would look like

public OperacaoDTO(String stagings, Double valor){
  this.stagings=stagings;
  this.valor=valor;
}

You have created a constructor with a list

List<Staging> stagings, BigDecimal valor,

Your query does not return this but an object with two attributes.

  • More, if DTO is of type @Entity must have the constructor empty as well, hibernate requirement
27.09.2016 / 02:38