Use SELECT NEW JPQL to popular DTO in a @Onetomany relationship

Asked

Viewed 1,551 times

1

Talk to the guys! I am trying to popular my DTO object, whose main object has a Onetomany relationship. Follow the code for vcs observations:

## Mapped Object:

public class Usuario implements Serializable, EntidadeBase {

private static final long serialVersionUID = -8398635491226829486L;

@Id @GeneratedValue(
        strategy=GenerationType.SEQUENCE, 
        generator="usuario_seq")
private Long id;

@Column(length=80, nullable=false)
private String nmUsuario;

@Column(length=80, nullable=false)
private String dsEmail;

@Column(nullable=false, columnDefinition="bit(1) default 0")
private Boolean flAtivo;

**@OneToMany(mappedBy="usuario", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Agendamento> listaAgendamentos;**

@ManyToOne
@JoinColumn(name="plano_fk", referencedColumnName="id", nullable=true)
private Plano plano;

public Usuario() {
    listaAgendamentos = new ArrayList<>();
}
...

## DTO with the builders:

public class UsuarioDTO implements Serializable {

private static final long serialVersionUID = 8399655567722875347L;

private Long id;
private String nmUsuario;
private String dsEmail;
private Boolean flAtivo;
private Collection<AgendamentoDTO> listaAgendamentos;

public UsuarioDTO(Long id, String nmUsuario, String dsEmail, Boolean flAtivo, **Collection<AgendamentoDTO> listaAgendamentos**) {
    super();
    this.id = id;
    this.nmUsuario = nmUsuario;
    this.dsEmail = dsEmail;
    this.flAtivo = flAtivo;
    //this.listaAgendamentos = listaAgendamentos;
    adicionarListaAgendamento(listaAgendamentos);
}

public UsuarioDTO() {
    listaAgendamentos = new ArrayList<>();
}
... 

## Query to popular DTO.

public UsuarioDTO getByEmail(String email){

    EntityManager em = JpaUtil.getEntityManager();
    UsuarioDTO dto = null;

    StringBuffer jpql = new StringBuffer()
    .append("select new br.com.marecrescente.dto.UsuarioDTO(user.id, user.nmUsuario, user.dsEmail, user.flAtivo, **user.listaAgendamentos**) "
            + "from Usuario user where user.dsEmail= :dsEmail");
    TypedQuery<UsuarioDTO> query = em.createQuery(jpql.toString(), UsuarioDTO.class);
    query.setParameter("dsEmail", email);

    List<UsuarioDTO> usuarios = query.getResultList();
    if(!usuarios.isEmpty()){
        dto = usuarios.get(0);
    }   

    return dto;
}

## Error running List users = query.getResultList();

Caused by: com.mysql.jdbc.exceptions.jdbc4.Mysqlsyntaxerrorexception: You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near 'as col_4_0_ from User User listaagend1_on usuari' at line 1 at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source)

I have looked everywhere and no response. If you can help me, I would be very grateful. Abs.

  • Hello! What is the reason of **? Remove them to be sure of the code used. As you are having a syntax problem, this clarification is important.

  • I have the same problem. Found the solution?

  • What was the generated query?

No answers

Browser other questions tagged

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