Brak rekordu w tabeli mapującej

0

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:
title title
title

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:
title

0

Nikt nie ma pomysłu?

0

Zrób save na author2 wyżej. Zaraz po utworzeniu. Swoją drogą masz nietypową konfigurację. Gdzie zamykasz sesję hiberbate?

0

jak przenoszę save author2 wyżej to dostaję org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

Musi być na końcu.

"gdzie zamykam sesję hibernate"? Nie wiem gdzie, nie zamykam.
właściwości mam takie:

spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

a poma takiego:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xxx.spring</groupId>
	<artifactId>springtraining1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springtraining1</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

0

w klasie Book mam nadpisaną metodę equals:

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (id != other.id)
			return false;
		return true;
	}

Gdy go usunę to wszystko działa jak należy :O

0

czyli tak jakby jpa pozwalał wpisywać tylko unikatowe książki do tabeli mapującej author_book

0

A jakie id ma obiekt book po utworzeniu? :)
Polecam:

1 użytkowników online, w tym zalogowanych: 0, gości: 1