I have two tables book and page I' trying to setup a bidirectional one to many relation between them.
page has unique constraint on multiple columns which isn't working correctly. When i insert book_item with unique page_item it works as its supposed to but when i try to delete the book which should also delete the pages, i get unique constraint violation exception which i don't really understand. i'm using jpa repository to perform insert/delete.
When i remove @UniqueConstraint the delete works but also the constraint doesn't so i don't really understand what i'm doing wrong.
@Entity
@Table(name = "book")
@NoArgsConstructor
@RequiredArgsConstructor
public class Book implements Serializable {
@Id
@GeneratedValue(generator = "book_uuid")
@GenericGenerator(name = "book_uuid", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(255)")
@Type(type="uuid-char")
@Getter
private UUID id;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
@Getter
@Setter
private List<Page> pages;
}
@Entity
@Table(name = "page", uniqueConstraints = @UniqueConstraint(columnNames = {"book_id", "chapter", "volume", "name"}))
@NoArgsConstructor
@RequiredArgsConstructor
public class Page implements Serializable {
@Id
@GeneratedValue(generator = "page_uuid")
@GenericGenerator(name = "page_uuid", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(255)")
@Type(type="uuid-char")
@Getter
private UUID id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", nullable = false)
@Getter
@Setter
@NonNull
private Book book;
@Column(name = "chapter", nullable = false)
@Getter
@Setter
private int chapter = 0;
@Column(name = "volume", nullable = false)
@Getter
@Setter
private int volume = 0;
@Column(name = "name", nullable = false)
@Getter
@Setter
@NonNull
private String name;
}
// Code used for insertion/update /deletion
@Repository
public interface BookRepository extends JpaRepository<Book, UUID> {
}
public void test() {
Book book = bookRepository.findById("9c7b2ab2-1c78-4e9f-adc5-99c7da42a7c6");
List<Page> pages = IntStream.range(0, 5)
.mapToObj(i -> new Page(book, "Page" + i, ".png"))
.collect(Collectors.toList());
book.setPages(pages);
bookRepository.save(book);
bookRepository.delete(book);
The error message i get :
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["UKOBQWYIY89PIIE6GP2NB4PVQ8W_INDEX_2 ON PUBLIC.PAGE(BOOK_ID, CHAPTER, VOLUME, NAME) VALUES ('acb0bb92-be2f-4dd3-9653-98f8d890e6b4', 0, 0, 'Page0', 1)"; SQL statement:
insert into page (book_id, chapter, extension, name, volume, id) values (?, ??, ?, ?, ?, ?) [23505-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
您可以尝试deleteById
deleteById("9c7b2ab2-1c78-4e9f-adc5-99c7da42a7c6");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.