0

I have two data models - sermon and sermon session. A sermon session can contain many sermons, and a sermon can only have one sermon session:

Sermon

@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;

Sermon Session

@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;

I keep getting the following error message whenever I try to delete a sermon:

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]

How can I fix it? I should be able to delete a sermon without needing to delete the sermon session

Update

Added my delete method:

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
        );
}
methuselah
  • 12,766
  • 47
  • 165
  • 315

0 Answers0