To są fragmenty kodu:
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "author_id"),
inverseJoinColumns = @JoinColumn(name = "book_id"))
private Set<Book> books = new HashSet<>();
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String isbn;
@OneToOne
private Publisher publisher;
@ManyToMany(mappedBy = "books")
private Set<Author> authors = new HashSet<>();
private void initData() {
Publisher publisher1 = new Publisher("Tabor");
publisherRepository.save(publisher1);
// Magdo, Madziu, Magdaleno
Author author1 = new Author("Magda", "Magdaleńska");
Book book1 = new Book("Tytuł książki 1", "123456abc", publisher1);
author1.getBooks().add(book1);
book1.getAuthors().add(author1);
bookRepository.save(book1);
authorRepository.save(author1);
// Cygan
Author author2 = new Author("Cygan", "Śniady");
Book book2 = new Book("Jak sprzedać dywan", "222", publisher1);
Book book3 = new Book("Jak sprzedać dywan2", "2223", publisher1);
author2.getBooks().add(book2);
author2.getBooks().add(book3);
book2.getAuthors().add(author2);
book3.getAuthors().add(author2);
bookRepository.save(book2);
bookRepository.save(book3);
authorRepository.save(author2);
}
które generują następujące tabele:
W tej ostatniej brakuje jednego rekordu o wartościach: insert into author_book values(6, 5)
. Czemu taki insert się nie odpala?
Tak wygląda cała lista sqli, które odpala hibernate:
Hibernate: drop table author if exists
Hibernate: drop table author_book if exists
Hibernate: drop table book if exists
Hibernate: drop table publisher if exists
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table author (id bigint not null, first_name varchar(255), last_name varchar(255), primary key (id))
Hibernate: create table author_book (author_id bigint not null, book_id bigint not null, primary key (author_id, book_id))
Hibernate: create table book (id bigint not null, isbn varchar(255), title varchar(255), primary key (id))
Hibernate: create table publisher (id bigint not null, name varchar(255), primary key (id))
Hibernate: alter table author_book add constraint FKn8665s8lv781v4eojs8jo3jao foreign key (book_id) references book
Hibernate: alter table author_book add constraint FKg7j6ud9d32ll232o9mgo90s57 foreign key (author_id) references author
Hibernate: call next value for hibernate_sequence
Hibernate: insert into publisher (name, id) values (?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into book (isbn, title, id) values (?, ?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into author (first_name, last_name, id) values (?, ?, ?)
Hibernate: insert into author_book (author_id, book_id) values (?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into book (isbn, title, id) values (?, ?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into book (isbn, title, id) values (?, ?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into author (first_name, last_name, id) values (?, ?, ?)
Hibernate: insert into author_book (author_id, book_id) values (?, ?)
Kolumny tabeli author_book
nie muszą być unikatowe: