8

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

Rapalagui
  • 79
  • 2
  • 11
Fernando Carvalho
  • 393
  • 1
  • 3
  • 14

4 Answers4

24

Can you can try with

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

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

Ankit
  • 1,250
  • 16
  • 23
  • Already did annotate each getter manually with @JsonProperty... I'll try @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) – Fernando Carvalho Aug 27 '13 at 16:11
  • Yes I think that this error is because Jackson is trying to serialize before the object is fully loaded. Looks like the Advertisement and SessionT are lazy-loaded, i.e. when JSON serialization starts it's not yet loaded. Let me know if it fixes the issue. This is interesting. – Ankit Aug 27 '13 at 16:18
  • 1
    This will work in some cases when the lazy loaded collections have already been fetched from the DB, but will most likely produce unexpected results in other cases (exceptions, missing data, etc.). YMMV, but I'd recommend you checkout the jackson-datatype-hibernate module mentioned in my answer (and upgrade your version of Jackson since they moved from CodeHaus to FasterXML for newer versions). – Christophe L Aug 27 '13 at 16:48
  • 1
    This works for me but note that if you are using PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy() the annotation would be: @JsonIgnoreProperties({"hibernate_lazy_initializer", "handler"}) – mmeyer Jan 23 '14 at 19:19
3

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

treeno
  • 2,510
  • 1
  • 20
  • 36
2

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.

Christophe L
  • 13,725
  • 6
  • 33
  • 33
  • Now with your help I understand, what impact this can cause having this version of jackson in the project. I tried at first use this example above but it doesn't work. So now I can produce more... and the jackson is really few in all of my code, and obviusly with the grow of complexity the jackson will grow to... but in this fase I certainly will have help (more programmers)... For now I'm alone and the thing have to work just fine!! Thanks for all patience with me!! – Fernando Carvalho Sep 09 '13 at 11:56
1

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.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thank all of you for all support gave in this particularity!!! Now the project are almost on middle and another programmer with more experience will give me more support and I will show all of your comments to him and see if he can make all this changes suggested! – Fernando Carvalho Sep 24 '13 at 11:59
  • 1
    I tried your suggestion for a RESTful interface using Wildfly 8.1.0 and Hibernate 4.3.5.Final. It works great, thanks! – Gerrit Brouwer Aug 07 '15 at 08:06