-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5664e82
commit cf6a5ef
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.3.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>ru.sysout</groupId> | ||
<artifactId>manytomany</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
|
||
|
||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
hibernate-many-to-many/src/main/java/ru/sysout/SpringDataJpaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.sysout; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringDataJpaApplication { | ||
|
||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringDataJpaApplication.class, args); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
hibernate-many-to-many/src/main/java/ru/sysout/dao/AuthorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.sysout.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.model.Author; | ||
|
||
public interface AuthorRepository extends JpaRepository<Author, Long> { | ||
Author findByName(String name); | ||
} |
8 changes: 8 additions & 0 deletions
8
hibernate-many-to-many/src/main/java/ru/sysout/dao/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.sysout.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.model.Book; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long> { | ||
Book findByName(String name); | ||
} |
55 changes: 55 additions & 0 deletions
55
hibernate-many-to-many/src/main/java/ru/sysout/model/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ru.sysout.model; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@NoArgsConstructor | ||
@Data | ||
@Entity | ||
public class Author { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private Long id; | ||
private String name; | ||
public Author(String name){ | ||
this.name=name; | ||
} | ||
@ManyToMany (cascade = { | ||
CascadeType.PERSIST, | ||
CascadeType.MERGE | ||
}) | ||
@JoinTable(name = "author_book", | ||
joinColumns = @JoinColumn(name = "author_id"), | ||
inverseJoinColumns = @JoinColumn(name = "book_id") | ||
) | ||
private Set<Book> books=new HashSet(); | ||
|
||
public void addBook(Book book){ | ||
this.books.add(book); | ||
book.getAuthors().add(this); | ||
} | ||
|
||
public void removeBook(Book book){ | ||
this.books.remove(book); | ||
book.getAuthors().remove(this); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
|
||
if (!(o instanceof Author)) return false; | ||
|
||
return id != null && id.equals(((Author) o).getId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 31; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
hibernate-many-to-many/src/main/java/ru/sysout/model/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ru.sysout.model; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@NoArgsConstructor | ||
@Data | ||
@Entity | ||
public class Book { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private Long id; | ||
private String name; | ||
|
||
@ManyToMany(mappedBy = "books") | ||
private Set<Author> authors=new HashSet<>(); | ||
|
||
public Book(String name){ | ||
this.name=name; | ||
} | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
|
||
if (!(o instanceof Author)) return false; | ||
|
||
return id != null && id.equals(((Book) o).getId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 31; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
spring: | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
datasource: | ||
url: jdbc:h2:mem:testdb | ||
username: sa | ||
password: | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: create |
50 changes: 50 additions & 0 deletions
50
hibernate-many-to-many/src/test/java/ru/sysout/ManyToManyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ru.sysout; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.annotation.Commit; | ||
import ru.sysout.dao.AuthorRepository; | ||
import ru.sysout.dao.BookRepository; | ||
import ru.sysout.model.Author; | ||
import ru.sysout.model.Book; | ||
|
||
@DataJpaTest | ||
@Commit | ||
public class ManyToManyTest { | ||
@Autowired | ||
private AuthorRepository authorRepository; | ||
@Autowired | ||
private BookRepository bookRepository; | ||
|
||
|
||
@BeforeEach | ||
public void booksShouldBeAdded() { | ||
Author author1 = new Author("a1"); | ||
|
||
Book b1 = new Book("b1"); | ||
Book b2 = new Book("b2"); | ||
author1.addBook(b1); | ||
author1.addBook(b2); | ||
|
||
authorRepository.save(author1); | ||
|
||
Author author2 = new Author("a2"); | ||
author2.addBook(b1); | ||
authorRepository.save(author2); | ||
|
||
Assertions.assertEquals(2, authorRepository.count()); | ||
Assertions.assertEquals(2, bookRepository.count()); | ||
} | ||
|
||
@Test | ||
@DisplayName("при удалении автора из книги с Set должен выполняться один delete-оператор") | ||
public void whenDeleteAuthorFromBook_thenOneDeleteStatement() { | ||
Author author = authorRepository.findByName("a1"); | ||
Book book = bookRepository.findByName("b1"); | ||
author.removeBook(book); | ||
} | ||
} |