将Hibernate对象序列化为JSON时引发异常 [英] Exception thrown when serializing Hibernate object to JSON

查看:150
本文介绍了将Hibernate对象序列化为JSON时引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在使用Hibernate将小型数据库加载到一些代表表的类中,并与数据库进行交互. 很好,我真的可以看到所有结果...而且我没有任何空字段,所有这些都已使用.

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;
      }
    }

还有

    @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*/
    }

我的目标是从广告列表生成JSON并通过Http发送.

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

我正在使用jackson-all-1.9.11和JBoss Studios 6.01

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

任何人都可以帮助我吗?

Anyone can help me??

推荐答案

当Hibernate从数据库中加载对象时,它会返回代理对象,这些对象看起来像Advertisment或SessionT,但其中包含更多的东西"(以处理与会话,延迟加载的集合的内部状态等).

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.

有一个项目使Jackson能够与Hibernate实体一起工作.请参阅: https://github.com/FasterXML/jackson-datatype-hibernate .

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

这篇关于将Hibernate对象序列化为JSON时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆