Skip to content

Commit c2d3a97

Browse files
committed
JPA HSQL One to many
1 parent dcec639 commit c2d3a97

File tree

7 files changed

+235
-0
lines changed

7 files changed

+235
-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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
9+
<name>JPA One-To-Many Example</name>
10+
<description>JPA One-To-Many Example</description>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.1.4.RELEASE</version>
16+
<relativePath/>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<java.version>1.7</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-data-jpa</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.hsqldb</groupId>
31+
<artifactId>hsqldb</artifactId>
32+
<scope>runtime</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
46+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.hellokoding.jpa;
2+
3+
import com.hellokoding.jpa.model.Book;
4+
import com.hellokoding.jpa.model.BookCategory;
5+
import com.hellokoding.jpa.repository.BookCategoryRepository;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.CommandLineRunner;
10+
import org.springframework.boot.SpringApplication;
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
13+
import javax.transaction.Transactional;
14+
import java.util.Arrays;
15+
import java.util.HashSet;
16+
import java.util.Set;
17+
18+
@SpringBootApplication
19+
public class Application implements CommandLineRunner {
20+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
21+
22+
@Autowired
23+
private BookCategoryRepository bookCategoryRepository;
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(Application.class, args);
27+
}
28+
29+
@Override
30+
@Transactional
31+
public void run(String... strings) throws Exception {
32+
// save a couple of categories
33+
final BookCategory categoryA = new BookCategory("Category A");
34+
Set<Book> bookAs = new HashSet<>();
35+
bookAs.add(new Book("Book A1", categoryA));
36+
bookAs.add(new Book("Book A2", categoryA));
37+
bookAs.add(new Book("Book A3", categoryA));
38+
categoryA.setBooks(bookAs);
39+
40+
final BookCategory categoryB = new BookCategory("Category B");
41+
Set<Book> bookBs = new HashSet<>();
42+
bookBs.add(new Book("Book B1", categoryB));
43+
bookBs.add(new Book("Book B2", categoryB));
44+
bookBs.add(new Book("Book B3", categoryB));
45+
categoryB.setBooks(bookBs);
46+
47+
bookCategoryRepository.saveAll(Arrays.asList(categoryA, categoryB));
48+
49+
// fetch all categories
50+
for (BookCategory bookCategory : bookCategoryRepository.findAll()) {
51+
logger.info(bookCategory.toString());
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class Book{
7+
@Id
8+
@GeneratedValue(strategy = GenerationType.IDENTITY)
9+
private int id;
10+
11+
private String name;
12+
13+
@ManyToOne
14+
@JoinColumn(name = "book_category_id")
15+
private BookCategory bookCategory;
16+
17+
public Book() {
18+
19+
}
20+
21+
public Book(String name) {
22+
this.name = name;
23+
}
24+
25+
public Book(String name, BookCategory bookCategory) {
26+
this.name = name;
27+
this.bookCategory = bookCategory;
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 BookCategory getBookCategory() {
47+
return bookCategory;
48+
}
49+
50+
public void setBookCategory(BookCategory bookCategory) {
51+
this.bookCategory = bookCategory;
52+
}
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
@Table(name = "book_category")
8+
public class BookCategory {
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private int id;
12+
13+
private String name;
14+
15+
@OneToMany(mappedBy = "bookCategory", cascade = CascadeType.ALL)
16+
private Set<Book> books;
17+
18+
public BookCategory(){
19+
20+
}
21+
22+
public BookCategory(String name) {
23+
this.name = name;
24+
}
25+
26+
public int getId() {
27+
return id;
28+
}
29+
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Set<Book> getBooks() {
43+
return books;
44+
}
45+
46+
public void setBooks(Set<Book> books) {
47+
this.books = books;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
String result = String.format(
53+
"Category[id=%d, name='%s']%n",
54+
id, name);
55+
if (books != null) {
56+
for(Book book : books) {
57+
result += String.format(
58+
"Book[id=%d, name='%s']%n",
59+
book.getId(), book.getName());
60+
}
61+
}
62+
63+
return result;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.hellokoding.jpa.repository;
2+
3+
import com.hellokoding.jpa.model.BookCategory;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface BookCategoryRepository extends JpaRepository<BookCategory, Integer>{
9+
}
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)