Error when viewing postgres table using Jpa, Hibernate and Postgres

Asked

Viewed 165 times

1

I did a small project to test a small registration in a postgres table , using jpa, with Hibernate, I managed to make the registration class work, it inserts in the table without problems but I can’t 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 stills of the project.

Class Creationobject

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");

    }

}

Query class robjetospsql

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;
    }
  • Good morning Ivanildo, could you provide a longer stretch of stacktrace? This error is caused by more than one reason and only with the available stretch I can not point out the problem.

1 answer

0

The only problem I see is having attributes like java.lang.Number in class.

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

Number is an abstract class, based on the numerical types of Java, but which cannot be instantiated directly. Note that when creating the object to be saved in the database, you are passing instances Float. But how will Hibernate know what kind of number he should create when he gets the raw information from the database?

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

Browser other questions tagged

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