0

I am using hibernate in building a Restful Web Service (CRUD) in Java. The problem is that when I return the data (GET) in some table that has some kind of relationship, I get the following error:

HTTP Status 500 - Internal Server Error Type Exception Report Message java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

When removing the bidirectional mapping created by hibernate on either side (table) the server returns to normal operation. I use the Gson library to return and receive my requests in the JSON form. Would anyone know how to solve this? Thank you all for your help.

Here is a part of my controller code:

 @GET
   @Path("getConvenio")
   @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
   public String getConvenioList() {
      Gson gson = new Gson();
      List<Tbconveniado> l = new ArrayList();
      try {
         l = new ArrayList(op.getConveniadoList());

      } catch (Exception ex) {
         ex.printStackTrace();
      }

      return gson.toJson(l);
   }

Following is the implementation of the "getConveniadoList" method that is present in the ConvenioOperations class:

public class ConvenioOperations {

   public void setConvenio(Tbconveniado tb) {
      Session s = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = s.beginTransaction();
      s.saveOrUpdate(tb);
      tx.commit();
      s.close();

   }
   public List<Tbconveniado> getConveniadoList(){

      Session s = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = s.beginTransaction();
       List<Tbconveniado> l = null;
      Query q = s.createQuery("from Tbconveniado c");
      l = q.list();
      tx.commit();
      s.close();
      return l;
   }

}

Finally, the mapping and the class generated by hibernate to the table "Tbconveniado"

<hibernate-mapping>
    <class name="pojos.Tbconveniado" table="tbconveniado" catalog="sindicatodb" optimistic-lock="version" >
        <id name="idConveniado" type="int">
            <column name="idConveniado" />
            <generator class="assigned" />
        </id>
<!-- foreign key -->
        <many-to-one name="tbramo" class="pojos.Tbramo" fetch="select">
            <column name="ramo" not-null="true" />
        </many-to-one>
        <property name="nome" type="string">
            <column name="nome" length="100" />
        </property>
        <property name="dataConvenio" type="date">
            <column name="dataConvenio" length="10" />
        </property>
        <property name="dataLimite" type="date">
            <column name="dataLimite" length="10" />
        </property>
        <property name="endereco" type="string">
            <column name="endereco" length="100" />
        </property>
        <property name="bairro" type="string">
            <column name="bairro" length="100" />
        </property>
        <property name="cep" type="string">
            <column name="cep" length="20" />
        </property>
        <property name="telefone" type="string">
            <column name="telefone" length="20" />
        </property>
        <property name="cnpj" type="string">
            <column name="cnpj" length="15" />
        </property>
        <property name="cidade" type="string">
            <column name="cidade" length="100" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="status" />
        </property>
        <property name="email" type="string">
            <column name="email" length="100" />
        </property>
    </class>
</hibernate-mapping>

Java Class for XML above:

public class Tbconveniado  implements java.io.Serializable {

     private int idConveniado;
     private Tbramo tbramo;
     private String nome;
     private Date dataConvenio;
     private Date dataLimite;
     private String endereco;
     private String bairro;
     private String cep;
     private String telefone;
     private String cnpj;
     private String cidade;
     private Integer status;
     private String email;

    public Tbconveniado() {
    }
    public Tbconveniado(int idConveniado, Tbramo tbramo) {
        this.idConveniado = idConveniado;
        this.tbramo = tbramo;
    }
    public Tbconveniado(int idConveniado, Tbramo tbramo, String nome, Date dataConvenio, Date dataLimite, String endereco, String bairro, String cep, String telefone, String cnpj, String cidade, Integer status, String email) {
       this.idConveniado = idConveniado;
       this.tbramo = tbramo;
       this.nome = nome;
       this.dataConvenio = dataConvenio;
       this.dataLimite = dataLimite;
       this.endereco = endereco;
       this.bairro = bairro;
       this.cep = cep;
       this.telefone = telefone;
       this.cnpj = cnpj;
       this.cidade = cidade;
       this.status = status;
       this.email = email;
    }

    public int getIdConveniado() {
        return this.idConveniado;
    }

    public void setIdConveniado(int idConveniado) {
        this.idConveniado = idConveniado;
    }
    public Tbramo getTbramo() {
        return this.tbramo;
    }

    public void setTbramo(Tbramo tbramo) {
        this.tbramo = tbramo;
    }
    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
    public Date getDataConvenio() {
        return this.dataConvenio;
    }

    public void setDataConvenio(Date dataConvenio) {
        this.dataConvenio = dataConvenio;
    }
    public Date getDataLimite() {
        return this.dataLimite;
    }

    public void setDataLimite(Date dataLimite) {
        this.dataLimite = dataLimite;
    }
    public String getEndereco() {
        return this.endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
    public String getBairro() {
        return this.bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }
    public String getCep() {
        return this.cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }
    public String getTelefone() {
        return this.telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
    public String getCnpj() {
        return this.cnpj;
    }

    public void setCnpj(String cnpj) {
        this.cnpj = cnpj;
    }
    public String getCidade() {
        return this.cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }
    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
  • could you provide a [MCVE]? – efekctive Jul 18 '17 at 01:11
  • Take a look at this answer: https://stackoverflow.com/questions/13459718/could-not-serialize-object-cause-of-hibernateproxy , hope that help. – Abass A Jul 18 '17 at 03:03
  • Friend tried this solution and it did not work. I created the HibernateProxyTypeAdapter class and in my GET method I did the following: "GsonBuilder b = new GsonBuilder (); B.registerTypeAdapterFactory (HibernateProxyTypeAdapter.FACTORY); Gson gson = b.create (); List l = new ArrayList (); Try { L = new ArrayList (op.getConveniadoList ()); } Catch (Exception ex) { Ex.printStackTrace (); } Return gson.toJson (l);" Can you tell me what might be happening? – Rogério Oliveira Jul 21 '17 at 00:12

0 Answers0