我有 2 个实体。约会和项目。它们都是独立的,我们可以在多个约会中拥有多个项目。
约会类:
@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;
和项目类:
@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 的列表中添加了一个项目,但保存不会将更改闪存到数据库中。
这是数据库:
知道我缺少什么吗?非常感谢,女士们,先生们。