2
2
.
.
7
7
.
.
3
3
@
@
O
O
n
n
e
e
T
T
o
o
M
M
a
a
n
n
y
y
-
-
J
J
o
o
i
i
n
n
T
T
a
a
b
b
l
l
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to implement @OneToMany relationship by using Join Table AUTHOR_BOOKS.
This table will hold information which Author has written which Books.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
DB Schema
Syntax
@OneToMany(cascade = CascadeType.ALL)
public Address address;
AUTHOR
NAME
AGE
BOOK
ID
BOOKS_ID
AUTHOR_BOOKS
AUTHOR_ID
MyController
http://localhost:8080/AddAuthor
Author
AuthorRepository
addAuthor()
Book
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: relationships_onetomany (add Spring Boot Starters from the table)
Edit FIle: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
Create Class: Author.java (inside package entities)
Create Class: Book.java (inside package entities)
Create Package: repositories (inside main package)
Create Interface: AuthorRepository.java (inside package repositories)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
application.properties
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
Author.java
package com.ivoronline.relationships_onetomany.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import java.util.Set;
@Entity
public class Author {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//RELATIONSHIPS
@OneToMany(cascade = CascadeType.ALL)
public Set<Book> books;
//DATA
public String name;
public Integer age;
}
AuthorRepository.java
package com.ivoronline.relationships_onetomany.repositories;
import com.ivoronline.relationships_onetomany.entities.Author;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Integer> { }
Book.java
package com.ivoronline.relationships_onetomany.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//DATA
public String title;
}
MyController.java
package com.ivoronline.relationships_onetomany.controllers;
import com.ivoronline.relationships_onetomany.entities.Author;
import com.ivoronline.relationships_onetomany.entities.Book;
import com.ivoronline.relationships_onetomany.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashSet;
@Controller
public class MyController {
@Autowired
AuthorRepository authorRepository;
@ResponseBody
@RequestMapping("/AddAuthor")
public String addAuthor() {
//CREATE BOOK ENTITIES
Book book1 = new Book();
book1.title = "Book about dogs";
Book book2 = new Book();
book2.title = "Book about cats";
//CREATE AUTHOR ENTITY
Author author = new Author();
author.name = "John";
author.age = 20;
author.books = new HashSet<Book>();
author.books.add(book1);
author.books.add(book2);
//STORE AUTHOR/BOOKS ENTITIES
authorRepository.save(author);
//RETURN SOMETHING TO BROWSER
return "Author & Books were stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddAuthor
AUTHOR BOOK http://localhost:8080/h2-console
AUTHOR_BOOKS