JPA returning Object different from class object type

Asked

Viewed 126 times

2

The following query returns java.lang.Classcastexception: [Ljava.lang.Object; cannot be cast to br.com.satisfacer.model.Client

public List<Cliente> teste(Cliente cliente) {
    return manager.createQuery("from Cliente c join c.compras e where e.dataUltimaCompra > '01.08.2018'")
            .setMaxResults(100).getResultList();
}

My Bean

@SessionScoped
@Named
public class EmailModuleBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private ClientePU clientePU;

    public void testes() {
        for (Cliente c : clientePU.teste(new Cliente())) {
            System.out.println(c.getNome());
        }
    }
}

My model:

@javax.persistence.Entity
@Table(name = "clientes")
public class Cliente implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "cod_cliente")
    private Long codigo;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "cod_profissao", referencedColumnName = "cod_profissao")
    private ClienteProfissao profissao;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente")
    private List<Compra> compras = new ArrayList<>();

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "cod_cliente", referencedColumnName = "cod_cliente")
    @NotFound(action = NotFoundAction.IGNORE)
    private ClienteOutrosDados outrosDados;

1 answer

2


tries so because when doing Join without a select the query tries to return a customer and a purchase

public List<Cliente> teste(Cliente cliente) {
    return manager.createQuery("select c from Cliente c join c.compras e where e.dataUltimaCompra > '01.08.2018'")
            .setMaxResults(100).getResultList();
}

Browser other questions tagged

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