杰克逊双向关系(一对多)不起作用 [英] Jackson bidirectional relationship (One-to-many) not working
问题描述
在此Web服务项目中,我正在使用Spring(xml + annotations)和Hibernate(annotations).数据库关系图,模型,预期和实际输出如下所示,
I'm using Spring(xml+annotations), Hibernate(annotations) in this web service project. The database relationship diagram, models, expected and actual output are given below,
Customer.java
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<Loan> loans = new HashSet<Loan>();
//constructors, getters and setters
}
Loan.java
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<Item> items = new ArrayList<Item>();
//constructors, getters, setters
}
Item.java
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
}
实际输出:此处未显示客户详细信息
Actual output:Here, customer details are not shown
{
"loanId":4,
"date":1484937000000,
"amount":10000.0,
"items":[
{
"itemId":3,
"name":"Item1",
"weight":10.0
},
{
"itemId":4,
"name":"Item2",
"weight":20.0
}
]
}
预期输出:寻找贷款时也需要显示客户详细信息
Expected output: need to display customer details also when looking for a loan
{
"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 预先感谢.
I can able to fetch the customer details from the database and fail to load it using Jackson Json. If I remove @JsonManagedReference, I end up with circular loop. If I remove @JsonBackReference, no effects in the output. Complete code at: https://github.com/liwevire/TM_Service Thanks in advance.
推荐答案
由于在Loan
实体的Customer
属性上使用了@JsonBackReference
,因此Customer
对象将不包括在序列化中.将@JsonManagedReference
用于Loan
对象中的Customer
,并将@JsonBackReference
用于Customer
实体中的Loan
属性.
Because you are using the @JsonBackReference
on the Customer
property in the Loan
entity, the Customer
object will not included in the serialization. Use the @JsonManagedReference
for the Customer
in the Loan
object and use @JsonBackReference
on the Loan
property in the Customer
entity.
这将序列化Loan
实体的Customer
属性.但是Customer
对象序列化将不包含Loan
属性.您需要选择关系的一侧进行序列化.
This will serialize the Customer
property of your Loan
entity. But the Customer
object serialization will not contains the Loan
property. You need to pick one side of the relationship to serialize.
要允许双方,请在您的实体中使用@JsonIdentityInfo
批注,并删除@JsonBackReference
和@JsonManagedReference
.您的实体将类似于:
To allow both side, use @JsonIdentityInfo
annotation in your entity and remove the @JsonBackReference
and @JsonManagedReference
. You entities will be something like:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "customerId")
public class Customer implements Serializable {
...
}
@JsonIdentityInfo
的property
引用您的实体ID属性,对于Customer
,这将是customerId
.还要对Loan
和Item
执行此操作.
The property
of the @JsonIdentityInfo
refer to your entity id property, for Customer
this will be customerId
. Do this for Loan
and Item
also.
这篇关于杰克逊双向关系(一对多)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!