hibernate - jackson 双向关系(一对多)不起作用

标签 hibernate spring-mvc jackson one-to-many circular-reference

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

Database Table relationship

客户.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<Loan> loans = new HashSet<Loan>();
    //constructors, getters and setters
}

贷款.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<Item> items = new ArrayList<Item>();
    //constructors, getters, setters
}

项目.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
提前致谢。

最佳答案

因为您使用的是@JsonBackReferenceCustomer属性(property)在Loan实体,Customer对象不会包含在序列化中。使用 @JsonManagedReferenceCustomerLoan对象和使用 @JsonBackReferenceLoan属性(property)在Customer实体。

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

要允许双方,请使用 @JsonIdentityInfo实体中的注释并删除 @JsonBackReference@JsonManagedReference .您的实体将类似于:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "customerId")
public class Customer implements Serializable {
    ...
}
property@JsonIdentityInfo引用您的实体 ID 属性,用于 Customer这将是 customerId .为 Loan 执行此操作和 Item还。

关于hibernate - jackson 双向关系(一对多)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778386/

相关文章:

java - Spring 中的 ResponseEntity 和 HttpEntity 有什么区别?

java - 在 Spring MVC 中,无法使用 Jackson @JsonFormat 将输入绑定(bind)到日期字段

java - Spring中使用注解与XML配置的情况?

json - Spring MVC @Scope 代理 bean 和 Jackson 2

java - 使用 @OneToOne 进行双向关系时为 "org.hibernate.TypeMismatchException: Provided id of the wrong type"

hibernate - 使用 spring boot 和 cloud foundry 将 Redis 集成为非平台 oracle 数据库的 Hibernate 二级缓存

java - 使用 Jackson 反序列化非字符串映射键

java - @JsonCreator 'Could not find creator property with name' 即使 ignoreUnknown = true

java - CAS - Spring Security 3.1 书

java - Hibernate更新对象的非引用字段