0

While writing my application I came across a problem with executting SQL statement. I tried to look for a solution on the net, but none of the found helped and I still do not know how to deal with an error I get. Here is exception I get:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKAM8LLDERP40MVBBWCEQPU6L2S: PUBLIC.BOOK_CATEGORY FOREIGN KEY(CATEGORY_ID) REFERENCES PUBLIC.CATEGORY(ID) (2)"; SQL statement:
insert into book_category (book_id, category_id) values (?, ?) [23506-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

And this is how the classes looks: Book.class

@Entity
@Table(name = "book")
public class Book {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;
 @Column(nullable = false)
 private String title;
 private String description;
 @Column(name = "release_date")
 @Temporal(TemporalType.TIMESTAMP)
 private Date releaseDate;
 @JoinColumn(name = "cover_image")
 @OneToOne(cascade = CascadeType.MERGE)
 private UploadFile coverImage;
 @OneToOne(cascade = CascadeType.MERGE)
 private UploadFile content;
 @ManyToMany
 @JoinTable(name = "book_category", joinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")},
 inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")})
 private Set<Category> categories;
 // constructors, setters, getters
}

Category.class

@Entity
@Table(name = "category")
public class Category {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;
 @Column(name = "category_name")
 private String name;
 @ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY)
 private List<Book> books;
 // ...
}
asked Sep 25, 2018 at 10:01
1
  • 4
    Did you read the error? It's telling you that you are violating the constraint named FKAM8LLDERP40MVBBWCEQPU6L2S. It's also telling you how this constraint works so that you can identify the error in the query. Commented Sep 25, 2018 at 10:04

1 Answer 1

1

Your are missing cascade definitions there.

Your Book Category gets written before a referenced entity (book or category) gets inserted first. This is not allowed due to the constraint you specified. The solution is to define

cascade = CascadeType.ALL

On both sides of the join (both in books and in categories). This is going to ensure that JPA persists all entities in the right order.

answered Sep 25, 2018 at 10:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.