与JsonBackReference&的两种关系JsonManagedReference [英] Two way relationship with JsonBackReference & JsonManagedReference

查看:106
本文介绍了与JsonBackReference&的两种关系JsonManagedReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案

我将解决与此博客有关的问题 Jackson –双向关系

I'll solve my problem regarding to this blog Jackson – Bidirectional Relationships

谢谢.

更新2

问题与 JsonBackReference JsonManagedReference 注释有关. 通过我的两种方式的关系,我必须明确选择一种使用JsonBackReference和JsonManagedReference进行序列化的方式. 但是在这里,我想以特定方式使用相反的方式" Parent-> Child "(默认情况下使用方式" Child-> Parent ") 当我颠倒这两个注释时,对于特殊要求,我正在寻找我的JSON. 关于如何在双向关系中使用JACKSON的任何想法吗?

The problem is about JsonBackReference and JsonManagedReference annotations. With my two way relationship, I have to explicitly select one way for serialization with JsonBackReference and JsonManagedReference. But here, I am in case to use the opposit way "Parent->Child" for a specific requierement (using the way "Child->Parent" by default) When I inversed those two annotations, my JSON is what I'm looking for, for the special requierment. Any idea on how to use JACKSON in a two way relationship ?

谢谢.

更新1

这是使用EntityGraph的简单代码(感谢@NeilStockton的建议),但是仍然不序列化JSON中的lazy属性:-(

Here is a code simple using EntityGraph (thanks to @NeilStockton suggestion), but still don't serialize the lazy attribute in JSON :-(

父母

@Entity
public class Parent {
    @Id
    @GeneratedValue
    private Long id;

    @column
    private String parentAttribute;

    @OneToOne(mappedBy = "parent", optional = false)
    @JsonBackReference
    private Child child;

孩子

@Entity
public class Child {
    @Id
    @GeneratedValue
    private Long id;

    @column
    private String childAttribute;

    @OneToOne(optional = false, cascade = CascadeType.ALL)
    @JsonManagedReference
    private Parent parent;

父存储库

public interface ParentRepository extends CrudRepository<Parent> {

    @EntityGraph(attributePaths = { "child" })
    //a hack to use findAll with default lazy/eager mapping
    Collection<Parent> findByIdNotNull(); 
}

生成的查询:

Hibernate: 
    select
        parent0_.id as id1_33_0_,
        child1_.id as id1_32_1_,
        parent0_.parent_attribute as parent_attribute2_33_0_,
        child1_.child_attribute as child_attribute2_32_1_,
    from
        test.parent parent0_ 
    left outer join
        test.child child1_ 
            on parent0_.id=child1_.parent_id 
    where
        parent0_.id is not null

JSON(没有孩子):

   [ {
    "id": 1
    "parentAttribute": "I am the parent"
    } ]

关于如何强制Jackson Hibernate4Module进行序列化的任何想法(如果存在)? 谢谢.

Any idea on how to force Jackson Hibernate4Module to serialize if present ? Thank you.

我有一个使用JPA/hibernate映射实体的Spring Boot 1.3.1后台.前端是Angular2应用程序.通信是REST/JSON. 我的问题是当我有一个懒惰的关系时,在某些查询中强制加载EAGER. 使用JOIN FETCH的解决方案在DAO层(存储库)中为我提供了帮助. 该实体现在已完全按照我希望在控制器层中的单个查询中加载. 但是由于Hibernate4Module,序列化的JSON仍不完整.

I have a Spring Boot 1.3.1 back-office using JPA/hibernate for mapping entities. The front-end is an Angular2 application. The communication is a REST/JSON. My question is about forcing EAGER loading in some queries when I have a Lazy relationship. The solution using JOIN FETCH helped me in DAO layer (Repositories). The entity is now completely loaded in a single query as I want in controllers layer. But the serialized JSON still incomplete due to Hibernate4Module.

Hibernate4Module的以下功能无法帮助:-(

Bellow Hibernate4Module features can't help :-(

  • FORCE_LAZY_LOADING
  • USE_TRANSIENT_ANNOTATION
  • SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS
  • REQUIRE_EXPLICIT_LAZY_LOADING_MARKER
  • REPLACE_PERSISTENT_COLLECTIONS

任何想法都值得欢迎.谢谢.

Any idea is welcome. Thanks.

推荐答案

最后,我通过在查询的select部分中使用带有构造函数的Custom Projection解决了我的问题.在新的投影类中,没有"JsonIgnore"或任何JPA注释,这些注释不会使Jackson序列化字段.我在该投影中添加了更多数据以供触及使用. 希望对您有所帮助.

Finally, I solved my problem by using Custom Projection with a constructor in the select part of the query. In the new projection class, there is no "JsonIgnore" or any JPA annotation that make field not serialized by Jackson. I added more data in that projection for reach use. Hope it'll help.

这篇关于与JsonBackReference&amp;的两种关系JsonManagedReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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