Spring 没有在数据库中保存多对多映射
Spring not saving many-to-many mapping in DB
我有 2 个实体。一个约会和一个项目。它们都是独立的,我们可以在多个约会中拥有多个项目。
约会class:
@Entity(name = "Appointment")
@Table(name = "appointment")
public class Appointment
{
@Id
@JsonProperty("appointment_id")
@Column(name = "appointment_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonProperty("date")
private Date startingTimestamp;
@ManyToMany(mappedBy = "appointment",
cascade = CascadeType.ALL)
private List<Item> collectionOfItems;
@JsonProperty("duration")
private int duration;
@JsonProperty("state")
private AppoitmentState state;
@ManyToOne
@JsonBackReference
@JoinColumn(name="user_appointment_owner_id", nullable = false)
@JsonProperty("user_owner")
private User user;
和项目 class:
@Entity
@Table(name = "item")
public class Item
{
@Id
@Column(name = "item_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@JsonProperty("name")
private String name;
@JsonProperty("duration")
private int duration;
@ManyToMany(targetEntity = Appointment.class,
cascade = CascadeType.ALL)
@JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "item_id") },
inverseJoinColumns = { @JoinColumn(name = "appointment_id") })
private List<Appointment> appointment;
@ManyToMany
@JsonProperty("set_of_professionals")
@JsonIgnore
private Set<Professional> professional;
@JsonProperty("business_owning")
private Long business_id;
此处省略了构造函数、getter 和 setter。
Appointment控制器内部也有patch方法。
@RequestMapping(value = "appointment/{itemID}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Appointment> addItem(@PathVariable Long itemID, @RequestBody ObjectNode appointment_id_str)
{
Long appointment_id = appointment_id_str.get("appoitment_id").asLong();
Optional<Appointment> targetAppoitment = appointmentService.findById(appointment_id);
Optional<Item> addedItem = itemService.findById(itemID);
if (targetAppoitment.isPresent() && addedItem.isPresent())
{
Appointment appoitmentInDB = targetAppoitment.get();
appoitmentInDB.addItem(addedItem.get());
Appointment savedAppointment = appointmentService.save(appoitmentInDB);
return new ResponseEntity<>(savedAppointment, HttpStatus.CREATED);
}
return new ResponseEntity("", HttpStatus.INTERNAL_SERVER_ERROR);
}
现在虽然在调试器中看到,appoitment的列表中已经添加了一个项目,但保存不会
将更改闪存到数据库中。
这是数据库:
知道我错过了什么吗?
是因为Item
class自己的关系
如果您在 Appointment
class 中描述了关系并在 Item
class 中使用了 mappedBy
,您就不会遇到此问题。发生这种情况是因为 Hibernate 使用定义关系的 class 来维护它的关联。
要解决此问题,您应该按以下方式调整您的实体:
class Appointment {
...
@ManyToMany(targetEntity = Item.class,
cascade = CascadeType.ALL)
@JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "appointment_id") },
inverseJoinColumns = { @JoinColumn(name = "item_id") })
List<Item> collectionOfItems;
...
}
和
class Item {
...
@ManyToMany(mappedBy = "collectionOfItems",
cascade = CascadeType.ALL)
private List<Appointment> appoitment;
...
}
这个问题已经在Whosebug上得到解答,
我有 2 个实体。一个约会和一个项目。它们都是独立的,我们可以在多个约会中拥有多个项目。
约会class:
@Entity(name = "Appointment")
@Table(name = "appointment")
public class Appointment
{
@Id
@JsonProperty("appointment_id")
@Column(name = "appointment_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonProperty("date")
private Date startingTimestamp;
@ManyToMany(mappedBy = "appointment",
cascade = CascadeType.ALL)
private List<Item> collectionOfItems;
@JsonProperty("duration")
private int duration;
@JsonProperty("state")
private AppoitmentState state;
@ManyToOne
@JsonBackReference
@JoinColumn(name="user_appointment_owner_id", nullable = false)
@JsonProperty("user_owner")
private User user;
和项目 class:
@Entity
@Table(name = "item")
public class Item
{
@Id
@Column(name = "item_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@JsonProperty("name")
private String name;
@JsonProperty("duration")
private int duration;
@ManyToMany(targetEntity = Appointment.class,
cascade = CascadeType.ALL)
@JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "item_id") },
inverseJoinColumns = { @JoinColumn(name = "appointment_id") })
private List<Appointment> appointment;
@ManyToMany
@JsonProperty("set_of_professionals")
@JsonIgnore
private Set<Professional> professional;
@JsonProperty("business_owning")
private Long business_id;
此处省略了构造函数、getter 和 setter。
Appointment控制器内部也有patch方法。
@RequestMapping(value = "appointment/{itemID}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Appointment> addItem(@PathVariable Long itemID, @RequestBody ObjectNode appointment_id_str)
{
Long appointment_id = appointment_id_str.get("appoitment_id").asLong();
Optional<Appointment> targetAppoitment = appointmentService.findById(appointment_id);
Optional<Item> addedItem = itemService.findById(itemID);
if (targetAppoitment.isPresent() && addedItem.isPresent())
{
Appointment appoitmentInDB = targetAppoitment.get();
appoitmentInDB.addItem(addedItem.get());
Appointment savedAppointment = appointmentService.save(appoitmentInDB);
return new ResponseEntity<>(savedAppointment, HttpStatus.CREATED);
}
return new ResponseEntity("", HttpStatus.INTERNAL_SERVER_ERROR);
}
现在虽然在调试器中看到,appoitment的列表中已经添加了一个项目,但保存不会 将更改闪存到数据库中。
这是数据库:
知道我错过了什么吗?
是因为Item
class自己的关系
如果您在 Appointment
class 中描述了关系并在 Item
class 中使用了 mappedBy
,您就不会遇到此问题。发生这种情况是因为 Hibernate 使用定义关系的 class 来维护它的关联。
要解决此问题,您应该按以下方式调整您的实体:
class Appointment {
...
@ManyToMany(targetEntity = Item.class,
cascade = CascadeType.ALL)
@JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "appointment_id") },
inverseJoinColumns = { @JoinColumn(name = "item_id") })
List<Item> collectionOfItems;
...
}
和
class Item {
...
@ManyToMany(mappedBy = "collectionOfItems",
cascade = CascadeType.ALL)
private List<Appointment> appoitment;
...
}
这个问题已经在Whosebug上得到解答,