休眠和杰克逊懒惰序列化 [英] Hibernate and Jackson lazy serialization

查看:166
本文介绍了休眠和杰克逊懒惰序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate和Jackson来编写我的对象序列化项目。
我认为我理解它是如何工作的,但我无法设法实现它。



如果我理解的很好,只要一个关系获取模式设置为LAZY,如果你想要这个关系,你必须初始化它。



这是我的课程:

  @Entity 
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(schema =MDDI_ADMIN,name =MINIUSINE)
@ Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MiniUsine {

@Id
@Column(name =MINIUSINEID,nullable = false)
private int miniUsineID ;

@Column(name =NAME,length = 40,nullable = false)
私有字符串名称;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name =FluxID)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<通量GT; fluxs = new HashSet< Flux>();

以及所有getters和setters。

我也试过这个 JsonInclude.Include.NON_EMPTY 作为类的注解。还尝试了NON_NULL。
然而,杰克逊一直向我发送



com.fasterxml.jackson.databind.JsonMappingException:未能延迟初始化一个角色集合: MiniUsine.fluxs,没有会话或会话被关闭(通过引用链:java.util.ArrayList [0] - > MiniUsine [fluxs])



我将它序列化为: mapper.writeValueAsString(optMU);



使用Jackson 2.3 .2



感谢您的帮助

解决方案

据我了解,hibernate返回的实体对象是一个派生自实体类的代理。如果尝试访问事务外的惰性字段的getter方法,则会得到LazyInitializationException。
我想说的是将flux设置为空集并不会对您有任何帮助。

  private set< ;助焊剂> fluxs = new HashSet< Flux>(); 

Hibernate会重载getter,如果您尝试在事务之外访问它(这是杰克逊正在做的检查它是否为空),你会得到LazyInit错误。


I'm working on a project using Hibernate and Jackson to serialize my objects. I think I understand how it is suposed to work but I can't manage to make it works.

If I understand well, as soon as a relation fetch mode is set to LAZY, if you want this relation, you have to initialize it.

Here is my class :

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(schema="MDDI_ADMIN", name = "MINIUSINE")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class MiniUsine {

    @Id
    @Column(name="MINIUSINEID", nullable = false)
    private int miniUsineID;

    @Column(name = "NAME", length = 40, nullable = false)
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="FluxID")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Set<Flux> fluxs = new HashSet<Flux>();

And all getters and setters.

I've also tried this JsonInclude.Include.NON_EMPTY as class annotation. Also tried the NON_NULL. However, jackson keeps sending me

com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: MiniUsine.fluxs, no session or session was closed (through reference chain: java.util.ArrayList[0]->MiniUsine["fluxs"])

I'm serializing it with : mapper.writeValueAsString(optMU);

Using Jackson 2.3.2

Thanks for help

解决方案

As far as I understand, the entity object that hibernate returns is a proxy which derives from your entity class. If you try to access getter methods for lazy fields outside of a transaction, you get LazyInitializationException. The point I want to make is setting fluxs to empty set doesn't help you at all.

private Set<Flux> fluxs = new HashSet<Flux>();

Hibernate overloads the getter and if you try to access it outside of a transaction(which jackson is doing to check if it is empty), you get the LazyInit error.

这篇关于休眠和杰克逊懒惰序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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