Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception thrown when serializing Hibernate object to JSON

Well I'm using Hibernate to load a tiny database to some classes representing the tables and interact with the database. All fine, and I can really see all results... And I don't have any null field, all of them are been used.

Here I show the "main" class (table).

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

    import org.codehaus.jackson.annotate.JsonAutoDetect;
    import org.codehaus.jackson.annotate.JsonProperty;

    @JsonAutoDetect
    public class Advertisement {

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      public int id;
      public SessionT session;
      public int idRoom;
      public String image;

      public Advertisement() {

      }

      /* Getters and Setters */
      @JsonProperty
      public int getID() /* Get example */ {
          return this.id;
      }
    }

And also

    @JsonAutoDetect
    public class SessionT {

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      public int id;
      public int iStatus;
      public String sStatus;
      public Date dtDateStart;
      public Date dtDateEnd;
      public boolean bhide;

      /* Constructor, Getter and Setters*/
    }

My objective is generate a JSON from a LIST of Advertisement and send through Http.

ObjectMapper mapper = new ObjectMapper();System.out.println("mapper started!");
mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
response.getOutputStream().println(mapper.writeValueAsString(ads));

And for some reason I'm getting the following error:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0]->entities.slave.Advertisement["session"]->entities.slave.SessionT_$$_javassist_2["hibernateLazyInitializer"])

I'm using jackson-all-1.9.11 and JBoss Studios 6.01

Anyone can help me??

like image 873
Fernando Carvalho Avatar asked Aug 27 '13 16:08

Fernando Carvalho


1 Answers

Can you can try with

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

Or a rather simple approach is to annotate each getter manually with @JsonProperty

like image 73
Ankit Avatar answered Sep 30 '22 16:09

Ankit