反序列化休眠和杰克逊中的延迟加载 [英] deserialize lazy loading in hibernate and jackson

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

问题描述

有没有办法保持LAZY加载和使用id而不是POJO对象反序列化对象。



我有2个类,

类似于此

  public class User {
@Id
@JsonProperty
公共长ID;

@ManyToMany(
fetch = FetchType.EAGER,

@JoinTable(
name =User_EntityType,
joinColumns = @ JoinColumn(name =user_id),
inverseJoinColumns = @JoinColumn(name =type_id)

@JsonProperty
public Set< Type>类型;

}

公共类类型{
@Id
@JsonProperty
公共长ID;

@ManyToMany(
fetch = FetchType.EAGER,
mappedBy =types,
targetEntity = User.class

@ JsonProperty
public Set< User>用户;
}

数据类型工作得很好。我可以使用hibernate编写和阅读,没有问题。



然而,我希望能够用REST API返回一个User对象,所以我使用Jackson来反序列化它。问题是,当我这样做时,它会反序列化包含其他Type对象的User对象中的每个Type,并且会造成巨大的混乱。



可能只是返回Set of Long类型的ids而不是Set of type?

解决方案

是的,这是可能的,如果您使用的是Jackson 2.0,则使用对象标识功能。

如果您使用 @JsonIdentityInfo 注释标注类,那么Jackson将只输出一次对象;随后的引用将使用该ID代替。因此,只要类型输出一次,您的类型集就会被序列化为ID。当反序列化杰克逊将把ID变成对象。



在你的情况下,我认为你需要像这样注释Type类:

  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property =id)
public class Type {
...

请参阅 http://wiki.fasterxml.com/JacksonFeatureObjectIdentity 了解如何使用此功能的详细信息。


Is there a way to keep LAZY loading and deserialize the object using the id instead of the POJO object.

I have 2 class that are joined by a many-to-many relationship.

Something like this

public class User {
    @Id
    @JsonProperty
    public long id;

    @ManyToMany(
            fetch = FetchType.EAGER,
    )
    @JoinTable(
            name = "User_EntityType",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "type_id")
    )
    @JsonProperty
    public Set<Type> types;

}

public class Type {
    @Id
    @JsonProperty
    public long id;

    @ManyToMany(
            fetch = FetchType.EAGER,
            mappedBy = "types",
            targetEntity = User.class
    )
    @JsonProperty
    public Set<User> users;
}

The data type works just fine. I can write and read using hibernate with no issue.

However, I want to be able to return an User object with a REST API, so I'm using Jackson to deserialize it. The issue is when I do that, it deserialize every Type in the User object, which includes other Type objects, and it creates a huge mess.

Is it possible to instead just return the Set of Long type ids instead of Set of Type?

解决方案

Yes it is possible, if you're using Jackson 2.0, with the Object Identity feature.

If you annotate a class with the @JsonIdentityInfo annotation then Jackson will only output the object once; subsequent references will use the ID instead. So your set of types will be serialized as IDs, as long as the types have been output once. When deserializing Jackson will turn the IDs back into objects.

In your case I think you would need to annotate your Type class like this:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Type {
    ...
}

See http://wiki.fasterxml.com/JacksonFeatureObjectIdentity for details on how to use this feature.

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

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