Skip to content

Commit e55d60b

Browse files
committed
JPA HSQL Many to many
1 parent c2d3a97 commit e55d60b

File tree

8 files changed

+265
-0
lines changed

8 files changed

+265
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
*.iml
3+
target
4+
*.DS_Store
5+
.mvn
6+
mvn*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.hellokoding</groupId>
6+
<artifactId>jpa</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>JPA Many-To-Many Example</name>
9+
<description>JPA Many-To-Many Example</description>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.1.4.RELEASE</version>
15+
</parent>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<java.version>1.7</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hsqldb</groupId>
29+
<artifactId>hsqldb</artifactId>
30+
<scope>runtime</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-maven-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.hellokoding.jpa;
2+
3+
import com.hellokoding.jpa.model.Book;
4+
import com.hellokoding.jpa.model.Publisher;
5+
import com.hellokoding.jpa.repository.BookRepository;
6+
import com.hellokoding.jpa.repository.PublisherRepository;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.CommandLineRunner;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
14+
import javax.transaction.Transactional;
15+
import java.util.Arrays;
16+
import java.util.HashSet;
17+
import java.util.Set;
18+
19+
@SpringBootApplication
20+
public class Application implements CommandLineRunner {
21+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
22+
23+
@Autowired
24+
private BookRepository bookRepository;
25+
26+
@Autowired
27+
private PublisherRepository publisherRepository;
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(Application.class, args);
31+
}
32+
33+
@Override
34+
@Transactional
35+
public void run(String... strings) throws Exception {
36+
// save a couple of books
37+
Publisher publisherA = new Publisher("Publisher A");
38+
Publisher publisherB = new Publisher("Publisher B");
39+
Publisher publisherC = new Publisher("Publisher C");
40+
Book bookA = new Book("Book A", new HashSet<>(Arrays.asList(publisherA, publisherB)));
41+
Book bookB = new Book("Book B", new HashSet<>(Arrays.asList(publisherA, publisherC)));
42+
bookRepository.saveAll(Arrays.asList(bookA, bookB));
43+
44+
// fetch all books
45+
for(Book book : bookRepository.findAll()) {
46+
logger.info(book.toString());
47+
}
48+
49+
// save a couple of publishers
50+
Book bookD = new Book("Book D");
51+
Book bookE = new Book("Book E");
52+
Book bookF = new Book("Book F");
53+
Publisher publisherD = new Publisher("Publisher D", new HashSet<Book>(Arrays.asList(bookD, bookE)));
54+
Publisher publisherE = new Publisher("Publisher E", new HashSet<Book>(Arrays.asList(bookE, bookF)));
55+
publisherRepository.saveAll(Arrays.asList(publisherD, publisherE));
56+
57+
// fetch all publishers
58+
for(Publisher publisher : publisherRepository.findAll()) {
59+
logger.info(publisher.toString());
60+
}
61+
}
62+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
public class Book{
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String name;
13+
14+
@ManyToMany(cascade = CascadeType.ALL)
15+
@JoinTable(name = "book_publisher", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "publisher_id", referencedColumnName = "id"))
16+
private Set<Publisher> publishers;
17+
18+
public Book() {
19+
20+
}
21+
22+
public Book(String name) {
23+
this.name = name;
24+
}
25+
26+
public Book(String name, Set<Publisher> publishers){
27+
this.name = name;
28+
this.publishers = publishers;
29+
}
30+
31+
public int getId() {
32+
return id;
33+
}
34+
35+
public void setId(int id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public Set<Publisher> getPublishers() {
48+
return publishers;
49+
}
50+
51+
public void setPublishers(Set<Publisher> publishers) {
52+
this.publishers = publishers;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
String result = String.format(
58+
"Book [id=%d, name='%s']%n",
59+
id, name);
60+
if (publishers != null) {
61+
for(Publisher publisher : publishers) {
62+
result += String.format(
63+
"Publisher[id=%d, name='%s']%n",
64+
publisher.getId(), publisher.getName());
65+
}
66+
}
67+
68+
return result;
69+
}
70+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
public class Publisher {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String name;
13+
14+
@ManyToMany(mappedBy = "publishers")
15+
private Set<Book> books;
16+
17+
public Publisher(){
18+
19+
}
20+
21+
public Publisher(String name){
22+
this.name = name;
23+
}
24+
25+
public Publisher(String name, Set<Book> books){
26+
this.name = name;
27+
this.books = books;
28+
}
29+
30+
public int getId() {
31+
return id;
32+
}
33+
34+
public void setId(int id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public Set<Book> getBooks() {
47+
return books;
48+
}
49+
50+
public void setBooks(Set<Book> books) {
51+
this.books = books;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
String result = String.format(
57+
"Publisher [id=%d, name='%s']%n",
58+
id, name);
59+
if (books != null) {
60+
for(Book book : books) {
61+
result += String.format(
62+
"Book[id=%d, name='%s']%n",
63+
book.getId(), book.getName());
64+
}
65+
}
66+
67+
return result;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.hellokoding.jpa.repository;
2+
3+
import com.hellokoding.jpa.model.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface BookRepository extends JpaRepository<Book, Integer>{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.hellokoding.jpa.repository;
2+
3+
import com.hellokoding.jpa.model.Publisher;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface PublisherRepository extends JpaRepository<Publisher, Long>{
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.jpa.hibernate.ddl-auto=create-drop
2+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)