Java 将 Hibernate 对象序列化为 JSON 时抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18470438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:53:37  来源:igfitidea点击:

Exception thrown when serializing Hibernate object to JSON

javahibernateHymanson

提问by Fernando Carvalho

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.

好吧,我正在使用 Hibernate 将一个小数据库加载到一些表示表的类并与数据库交互。一切都很好,我真的可以看到所有结果......而且我没有任何空字段,它们都被使用了。

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

这里我展示了“主”类(表)。

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

    import org.codehaus.Hymanson.annotate.JsonAutoDetect;
    import org.codehaus.Hymanson.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.

我的目标是从广告列表生成 JSON 并通过 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.Hymanson.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 Hymanson-all-1.9.11 and JBoss Studios 6.01

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

Anyone can help me??

谁能帮帮我??

采纳答案by Christophe L

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

当 Hibernate 从数据库加载对象时,它会返回代理对象,这些对象看起来像您的广告或 SessionT,但其中包含更多“内容”(处理它们与会话的关系、延迟加载集合的内部状态等)。

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

这抛弃了 Hymanson 序列化器,因为它依赖于自省来找到我们对象的属性。

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

有一个项目可以让 Hymanson 与 Hibernate 实体一起工作。请参阅:https: //github.com/FasterXML/Hymanson-datatype-hibernate

回答by Ankit

Can you can try with

你可以试试

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

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

或者一个相当简单的方法是使用 @JsonProperty 手动注释每个 getter

回答by treeno

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 Advertismentfrom DB, if I'm right.

如果您有一个与另一个实体有关系的实体,Hibernate 会用所谓的 PersistenceBag 替换列表或引用的类。这使得进行一些延迟加载成为可能。我认为这与您的会话属性有关。如果我是对的,您应该能够在从数据库加载广告后立即在调试器中看到它。

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

我不知道 Json 序列化程序如何处理这种休眠魔术。也许您必须首先将实体复制到 pojo dto(一个没有任何 Hibernate-Context 的类)中,然后将该 dto 序列化为 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...

但也许有更好的方法来做到这一点。您可以尝试停用延迟加载,但我不确定这是否真的改变了休眠行为...

回答by StaxMan

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

正如Christophe L上面提到的,使用Hibernate模块:https://github.com/FasterXML/Hymanson-datatype-hibernate-否则Hyman逊没有办法知道如何处理特定类型的第三方库的方式。

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

此外,Hymanson 2.x 对此类外部类型有更好的支持,因此如果可能的话,带有 Hibernate 模块的 Hymanson 2.2 将是更好的选择。但我明白升级并不总是容易或可能的。