У меня есть две модели данных - проповедь и сессия проповеди.Сессия проповеди может содержать много проповедей, и проповедь может иметь только одну сессию проповеди:
Проповедь
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false, unique = true)
@SuppressWarnings("unused")
private int id;
@Column(unique = true)
private String fileName;
private String name;
private String speaker;
private int duration;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sermon_session_id", referencedColumnName = "id")
private SermonSession sermonSession;
private LocalDate date;
private LocalDate uploadDate;
private String description;
@ElementCollection
@CollectionTable(
name = "SERMON_DATA_TAGS",
joinColumns = @JoinColumn(name = "id", referencedColumnName = "id"))
@Column(name = "tag")
private List<String> tags;
Сессия проповеди
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false, unique = true)
@SuppressWarnings("unused")
private int id;
@Enumerated(EnumType.STRING)
@Column(unique = true)
private SessionEnum sessionEnum;
При попытке удалить проповедь я получаю следующее сообщение об ошибке:
delete from sermon_session where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK7WS4Y2S081JPFSURR19K9WVCR: PUBLIC.SERMON_DATA FOREIGN KEY(SERMON_SESSION_ID) REFERENCES PUBLIC.SERMON_SESSION(ID) (2)"; SQL statement:
delete from sermon_session where id=? [23503-197]
Как это исправить?Я должен иметь возможность удалить проповедь без необходимости удалять сеанс проповеди
Обновление
Добавлен мой метод удаления:
CRUDResponse deleteSermon(int id, String host) {
if (sermonDataRepository.findById(id).isPresent()) {
SermonData deleteSermon = sermonDataRepository.findById(id).get();
if (activeProfile.equals("live")) {
awsService.deleteFileFromS3Bucket(deleteSermon.getFileName());
}
sermonDataRepository.delete(deleteSermon);
return new CRUDResponse(
deleteSermon,
HttpStatus.OK.value(),
String.format("%s%s/%d",
host,
applicationConfiguration.getRestSermonPath(),
deleteSermon.getId()),
Message.DELETED
);
} else
return new CRUDResponse(
null,
HttpStatus.NOT_FOUND.value(),
String.format("%s%s/%d", host, applicationConfiguration.getRestSermonPath(), id),
Message.NOT_FOUND
);
}