我正在尝试实施一个会议模型,其中包含多个具有相应数量的设备实体。 在会议上,用户应该能够CRUD设备和会议设备的数量
数据库:
CREATE TABLE IF NOT EXISTS equipment (
equipment_id SERIAL PRIMARY KEY,
equipment_name VARCHAR(20) NOT NULL
);
CREATE TABLE IF NOT EXISTS meeting (
meeting_id SERIAL PRIMARY KEY,
meeting_time TIMESTAMP NOT NULL,
number_people INTEGER NOT NULL,
setup VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS meeting_equipment (
meeting_equipment_id SERIAL PRIMARY KEY ,
meeting_id INTEGER NOT NULL REFERENCES meeting (meeting_id),
equipment_id INTEGER NOT NULL REFERENCES equipment (equipment_id),
quantity INTEGER NOT NULL DEFAULT 0
);
实体实施:
@Entity
@Table(name = "meeting")
@Data
public class Meeting {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "meeting_id", updatable = false)
@JsonIgnore
private int id;
@Column(name = "meeting_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
@NotNull
private LocalDateTime meetingTime;
@Column(name = "number_people")
@NotNull
@Min(1)
private int numberPeople;
@Column(name = "setup")
@NotNull
private String setup;
@OneToMany(mappedBy = "meeting", cascade = CascadeType.ALL)
@JsonManagedReference
List<MeetingEquipment> equipmentList = new ArrayList<>();
}
@Entity
@Table(name = "equipment")
@Data
public class Equipment {
@Id
@Column(name = "equipment_id", updatable = false)
@JsonIgnore
private int id;
@NotNull
@Column(name = "equipment_name", unique = true)
@Size(min = 1, max = 100)
private String equipmentName;
}
加入表metting_equipment:
@Entity
@Table(name = "meeting_equipment", uniqueConstraints = {
@UniqueConstraint(columnNames = {"meeting_id", "equipment_id"})})
@Data
public class MeetingEquipment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "meeting_equipment_id", updatable = false)
@JsonIgnore
private int id;
@ManyToOne
@JoinColumn(name = "meeting_id")
@NotNull
@JsonBackReference
private Meeting meeting;
@ManyToOne
@JoinColumn(name = "equipment_id")
@NotNull
private Equipment equipment;
@Column(name = "quantity")
@NotNull
private int quantity;
}
使用上面的代码,我可以成功创建包含设备的会议(从创建方法返回的JSON显示正确的内容)。但是一旦我尝试删除会议实体中的deviceList元素,它就不会删除meetingEquipment实体。我试过了
meeting.getEquipmentList().clear()
和meetingEquipmentDao.delete(meeting.getEquipmentList())
都不起作用。
谁能告诉我这个问题的原因,谢谢!