我試圖通過其他使用JSON一些數據添加到數據庫:如何通過CrudRepository在數據庫中保存包含另一個實體的新實體?
{
"id": null,
"basicInfo": {
"firstName": "Mistral",
"lastName": "Castrol",
"pesel": "45789623456",
"startDate": "2017-02-12"
},
"contactInfo": {
"id": null,
"email": "[email protected]",
"phoneNumber": "456789123",
"address": {
"houseNamber": 53,
"apartmentNumber": 25,
"street": {
"id": 1,
"name": "Koszmarna",
"city": {
"id": 2,
"name": "Radzyń Podlaski",
"voivodeship": {
"id": 2,
"name": "mazowieckie"
},
"zipCode": "21-307"
}
}
}
},
"bornInfo": {
"id": null,
"bornDate": "2018-02-14",
"city": {
"id": 1,
"name": "Siedlce",
"voivodeship": {
"id": 1,
"name": "śląskie"
},
"zipCode": "21-307"
}
}
}
。我失去耐心。很多小時google搜索和嘗試後,我卡在此錯誤:
"message": "detached entity passed to persist: pl.dn.model.placeInfo.City; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: pl.dn.model.placeInfo.City"
的問題是城市(分離)的實體已經存在於數據庫中,但喜歡的firstName另一個數據,二是transisent實體的一部分。我不知道我應該 modfify json或模型。
型號:
主持人
@Entity
public class Moderator {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Embedded
private BasicInfo basicInfo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "contact_info_id")
private ModeratorContactInfo contactInfo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "born_info_id")
private ModeratorBornInfo bornInfo;
}
ModeratorContactInfo
@Entity
@Table(name = "moderator_contact_info")
public class ModeratorContactInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String email;
@Column(name = "phone_number")
private String phoneNumber;
@Embedded
private Address address;
}
地址
@Embeddable
public class Address {
@Column(name = "house_number")
private long houseNamber;
@Column(name = "apratment_number")
private long apartmentNumber;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "street_id")
private Street street;
}
街
@Entity
public class Street {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "city_id")
private City city;
}
市
@Entity
@Table(name = "city")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "voivodeship_id")
private Voivodeship voivodeship;
@Column(name = "zip_code", length = 6)
private String zipCode;
}
省
@Entity
public class Voivodeship {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
}
最後控制器的方法:
@RequestMapping(value = "add", method = RequestMethod.POST)
public String addModerator(@RequestBody Moderator moderator) {
return moderatorDao.save(moderator).toString();
}
時序解決方案:
如果我從數據庫加載城市並將其設置爲用戶,它將起作用,但我必須處理版主中的所有實體。
@RequestMapping(value = "add", method = RequestMethod.POST)
public String addModerator(@RequestBody Moderator moderator) {
long bornCityId = moderator.getBornInfo().getCity().getId();
long contactCityId = moderator.getContactInfo().getAddress().getStreet().getCity().getId();
if (! (bornCityId == 0)) {
City city = cityDao.findById(bornCityId);
moderator.getBornInfo().setCity(city);
}
if (! (contactCityId == 0)) {
City city = cityDao.findById(contactCityId);
moderator.getContactInfo().getAddress().getStreet().setCity(city);
}
return moderatorDao.save(moderator).toString();
}