Create Dto by Constructor with a List parameter of a Hibernate entity

Asked

Viewed 824 times

4

I have a @Query Spring, which performs a database query by an Entity, to create some Objects DTO. And the properties of this DTO object, I pass via Constructor, but I needed to pass as parameter in Constructor one List that has in the Entity.

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

mine @Query

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

The problem he complains about is that he doesn’t have a proper builder in Operacaodto. Is there any way that List of the Entity and pass in the Builder? because I will perform some treatment in this List

  • Could you provide the complete error? How is the entity mapping Operacao?

1 answer

1

You must create a constructor as follows for the object

OperacaoDTO(operacao.stagings, operacao.valor)

The types must be equal to the Object, for example: if value is Double and stagings for String would look like this

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

You 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 Operacaodto be the type @Entity must have the manufacturer empty too, Hibernate requirement

Browser other questions tagged

You are not signed in. Login or sign up in order to post.