Error when querying postgres table using Jpa, Hibernate and Postgres

1

I made a small project to test a small register in a postgres table, using jpa, with hibernate, I managed to make the registration class work, it inserts into the table without problems but I can not make the query work because I get the following error:

  

Exception in thread "main" javax.persistence.PersistenceException:   org.hibernate.type.SerializationException: could not deserialize

follows the calasses of the project.

Class CreatingObject

package com.jpapgsql.main;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.jpapgsql.model.Grupos;

public class CriandoObjeto {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("gruposPU");
        EntityManager em = emf.createEntityManager();

        Grupos grupos = new Grupos();
        Float comvista = (float) 1.5;
        Float comprazo = (float) 2.0;
        grupos.setDescricaogrupo("GRUPO TESTE JPA 2");
        grupos.setComissaogrupoavista(comvista);
        grupos.setComissaogrupoaprazo(comprazo);

        em.getTransaction().begin();
        em.persist(grupos);
        em.getTransaction().commit();

        System.out.println("Registo Salvo com sucesso");

    }

}

Class QueryObjectsPsql

package com.jpapgsql.main;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


import com.jpapgsql.model.Grupos;

public class ConsultarObjetosPsql {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("gruposPU");
        EntityManager em = emf.createEntityManager();

        List<Grupos> grupos = em.createQuery("FROM Grupos", Grupos.class).getResultList();

        for ( Grupos grupo : grupos) {

            System.out.println("CODIGO: " + String.valueOf(grupo.getCodgrupo()));
            System.out.println("DESCRICAO: " + grupo.getDescricaogrupo());
            System.out.println("---------------------------------");

        }
    }

}

Mapping class

package com.jpapgsql.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tgrupos")
public class Grupos implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 767902368399049708L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codgrupo;
    private String descricaogrupo;
    private Number comissaogrupoavista;
    private Number comissaogrupoaprazo;

    public Long getCodgrupo() {
        return codgrupo;
    }
    public void setCodgrupo(Long codgrupo) {
        this.codgrupo = codgrupo;
    }
    public String getDescricaogrupo() {
        return descricaogrupo;
    }
    public void setDescricaogrupo(String descricaogrupo) {
        this.descricaogrupo = descricaogrupo;
    }
    public Number getComissaogrupoavista() {
        return comissaogrupoavista;
    }
    public void setComissaogrupoavista(Number comissaogrupoavista) {
        this.comissaogrupoavista = comissaogrupoavista;
    }
    public Number getComissaogrupoaprazo() {
        return comissaogrupoaprazo;
    }
    public void setComissaogrupoaprazo(Number comissaogrupoaprazo) {
        this.comissaogrupoaprazo = comissaogrupoaprazo;
    }
    
asked by anonymous 16.10.2015 / 20:40

1 answer

0

The only problem I see is that there are attributes of type java.lang.Number in the class.

This does not seem to be supported by Hibernate according to the documentation I found, for example in manual and no developer's guide .

Number is an abstract class, the basis of Java numeric types, but can not be instantiated directly. Notice that when you create the object to be saved in the database, you are passing Float instances. But how will Hibernate know what kind of number it should create when it picks up raw information from the database?

This type of error can be associated with any kind of attribute that Hibernate does not know how to handle or, as I have read in some forums, with incorrect annotations.

    
05.11.2015 / 05:10