当前位置:  开发笔记 > 编程语言 > 正文

杰克逊双向关系(一对多)不起作用

如何解决《杰克逊双向关系(一对多)不起作用》经验,为你挑选了1个好方法。

我在这个Web服务项目中使用Spring(xml + annotations),Hibernate(注释).数据库关系图,模型,预期和实际输出如下,

数据库表关系

Customer.java

@Entity
@Table(name="customer")
public class Customer implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="customer_id", unique=true, nullable =false)
    long customerId;
    @Column(name="name")
    String name;
    @Column(name="secondary_name")
    String secondaryName;
    @Column(name="date")
    Date date;
    @Column(name="address")
    String address;
    @Column(name="post")
    String post;
    @Column(name="pin")
    String pin;
    @Column(name="phone")
    String phone;
    @OneToMany(fetch=FetchType.LAZY, mappedBy="customer", cascade=CascadeType.ALL)
    @JsonManagedReference
    Set loans = new HashSet();
    //constructors, getters and setters
}

Loan.java

public class Loan implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="loan_id", nullable=false, unique=true)
    long loanId;
    @ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="customer_id", nullable = false)
    @JsonBackReference
    Customer customer;
    @Column(name="date", nullable=false)
    Date date;
    @Column(name="amount", nullable=false)
    double amount;
    @OneToMany(fetch=FetchType.LAZY, mappedBy="loan", cascade=CascadeType.ALL)
    @JsonManagedReference
    List items = new ArrayList();
    //constructors, getters, setters
}

Item.java

public class Item implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="item_id", nullable=false, unique=true)
    long itemId;
    @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="loan_id", nullable = false)
    @JsonBackReference
    Loan loan;
    @Column(name="name", nullable=false)
    String name;
    @Column(name="weight", nullable=false)
    double weight;
    //constructors, setters, getters
}

实际输出:此处未显示客户详细信息

{  
   "loanId":4,
   "date":1484937000000,
   "amount":10000.0,
   "items":[  
      {  
         "itemId":3,
         "name":"Item1",
         "weight":10.0
      },
      {  
         "itemId":4,
         "name":"Item2",
         "weight":20.0
      }
   ]
}

预期产出:在寻找贷款时也需要显示客户详细信息

{  
   "loanId":4,
   "customer":{  
      "customerId":2,
      "name":"Prem",
      "address":"Street,State"
   },
   "date":1484937000000,
   "amount":10000.0,
   "items":[  
      {  
         "itemId":3,
         "name":"Item1",
         "weight":10.0
      },
      {  
         "itemId":4,
         "name":"Item2",
         "weight":20.0
      }
   ]
}

我可以从数据库中获取客户详细信息,但无法使用Jackson Json加载它.如果我删除@JsonManagedReference,我最终会得到循环循环.如果我删除@JsonBackReference,则输出中没有效果.完整代码:https://github.com/liwevire/TM_Service 提前致谢.



1> W-S..:

因为您在实体中使用@JsonBackReferenceon Customer属性,所以LoanCustomer对象不会包含在序列化中.在对象中使用@JsonManagedReferencefor,并在实体中的属性上使用.CustomerLoan@JsonBackReferenceLoanCustomer

这将序列化Customer您的Loan实体的属性.但是Customer对象序列化将不包含该Loan属性.您需要选择关系的一侧进行序列化.

要允许双方,请@JsonIdentityInfo在实体中使用注释并删除@JsonBackReference@JsonManagedReference.你的实体将是这样的:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "customerId")
public class Customer implements Serializable {
    ...
}

property@JsonIdentityInfo参考您的实体ID属性,Customer这将是customerId.这样做对LoanItem也.


经过大量尝试,@ JsonIgnoreProperties解决了我的问题
推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有