我有一个与同一Parcel
类有两个关系的实体,但有不同的对象:
@Entity
public class Parcel implements Serializable
{
@Id
@GeneratedValue
@Column
private Long id;
@OneToOne( mappedBy = "parcelSender", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Place sender;
@OneToOne( mappedBy = "parcelRecipient", cascade = CascadeType.ALL, fetch = FetchType.EAGER )
private Place recipient;
//getters, setters, etc.
}
Place
实体:
@Entity
public class Place implements Serializable
{
@Id
@GeneratedValue
@Column
private Long id;
@OneToOne
@PrimaryKeyJoinColumn
private Parcel parcelSender;
@OneToOne
@PrimaryKeyJoinColumn
private Parcel parcelRecipient;
//getters, setters
}
如果你看到,我与@OneToOne
个对象有Place
个关系。一切都运行得很好 - 当我用REST持久化对象时,我得到JSON响应,其中发送者和接收者是正确的。当我想要补丁Parcels
对象时,我得到JSON响应,其中发送者和收件人指向相同的数据。
例如,我坚持对象:
{
"sender":{
"name": "John",
"email":"othermail@mail.com"
},
"recipient":{
"name": "Mike",
"email":"mymail@mail.com",
}
}
我得到持久数据的回复:
{
"id": 1,
"sender": {
"id": 4,
"name": "John",
"email": "othermail@mail.com"
},
"recipient": {
"id": 3,
"name": "Mike",
"email": "mymail@mail.com"
}
}
现在,当我想在Parcel实体中修补一些字段时,我在JSON reposne中有相同的对象:
我补丁(这是在parcel id之上):
{
"id": 1
//some not important data
}
并回复:
{
"id": 3,
"sender": {
"id": 3,
"name": "Mike",
"email": "mymail@mail.com"
},
"recipient": {
"id": 3,
"name": "Mike",
"email": "mymail@mail.com"
}
}
可能问题很简单,但我不知道如何解决。我真的不明白为什么即使他们在实体中有不同的mappedBy
,我也能获得相同的对象。我需要有两个@OneToOne
关系到同一个类但不同的对象。是否有可能以巧妙的方式解决这个问题?