简体   繁体   中英

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??

Can you can try with

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

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

If you have an Entity that has a relationship to another Entity, Hibernate replaces the List or the referenced Class with a so called PersistenceBag. This makes it possible to do some lazy loading. I think this happens with your session attribute. You should be able to see that in the debugger right after loading Advertisment from DB, if I'm right.

I don't know how the Json serializer handles this hibernate-magic. Maybe you have to copy your entity in a pojo dto (a class without any Hibernate-Context) first and serialize that dto to json...

But maybe there are better ways to do that. You could try to deactivate lazy-loading, but I'm not shure if this really changes hibernates behavior...

When Hibernate loads objects from the DB, it returns proxied objects which look like your Advertisment or SessionT but have more "stuff" in them (to handle their relationship to the session, the internal state of lazy loaded collections etc.).

This throws off the Jackson serializer since it relies on introspection to find our the properties of the objects.

There's a project to enable Jackson to work with Hibernate entities. See: https://github.com/FasterXML/jackson-datatype-hibernate .

As Christophe L above mentioned, use the Hibernate module: https://github.com/FasterXML/jackson-datatype-hibernate -- otherwise Jackson has no way of knowing how to handle types specific to a third-party library.

Also, Jackson 2.x has better support for such external types, so if at all possible, Jackson 2.2 with Hibernate module would be more optimal choice. But I understand that upgrade is not always easy or possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM