Skip to content

Commit 1c83716

Browse files
committed
Many-to-one and JoinFormula
1 parent 48ae444 commit 1c83716

File tree

9 files changed

+321
-0
lines changed

9 files changed

+321
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**[OneToMany Bidirectional](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootOneToManyBidirectional)**
2+
3+
**Description:** This application is a proof of concept of how is correct to implement the bidirectional `@OneToMany` association.
4+
5+
**Key points:**\
6+
- always cascade from parent to child\
7+
- use `mappedBy` on the parent\
8+
- use `orphanRemoval` on parent in order to remove children without references\
9+
- use helper methods on parent to keep both sides of the association in sync\
10+
- use lazy fetching on both side of the association\
11+
- as entities identifiers, use assigned identifiers (business key, natural key (`@NaturalId`)) and/or database-generated identifiers and override (on child-side) properly the `equals()` and `hashCode()` methods as [here](https://vladmihalcea.com/the-best-way-to-implement-equals-hashcode-and-tostring-with-jpa-and-hibernate/)\
12+
- if `toString()` need to be overridden, then pay attention to involve only for the basic attributes fetched when the entity is loaded from the database
13+
14+
**Note:** Pay attention to remove operations, especially to removing child entities. The `CascadeType.REMOVE` and `orphanRemoval=true` may produce too many queries. Relying on *bulk* operations is most of the time the best way to go for deletions.
15+
16+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="center"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="410" width="350"/></p></a>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>HibernateSpringBootJoinFormula</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootJoinFormula</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(MainApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
bookstoreService.fetchBooks();
26+
};
27+
}
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Author implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private String name;
19+
private String genre;
20+
private int age;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getGenre() {
39+
return genre;
40+
}
41+
42+
public void setGenre(String genre) {
43+
this.genre = genre;
44+
}
45+
46+
public int getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(int age) {
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Author{" + "id=" + id + ", name=" + name
57+
+ ", genre=" + genre + ", age=" + age + '}';
58+
}
59+
60+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.ManyToOne;
11+
import org.hibernate.annotations.JoinFormula;
12+
13+
@Entity
14+
public class Book implements Serializable {
15+
16+
private static final long serialVersionUID = 1L;
17+
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.IDENTITY)
20+
private Long id;
21+
22+
private String title;
23+
private String isbn;
24+
private int price;
25+
26+
@ManyToOne(fetch = FetchType.LAZY)
27+
@JoinColumn(name = "author_id")
28+
private Author author;
29+
30+
@ManyToOne(fetch = FetchType.LAZY)
31+
@JoinFormula("(SELECT b.id FROM book b "
32+
+ "WHERE b.price < price AND b.author_id = author_id "
33+
+ "ORDER BY b.price DESC "
34+
+ "LIMIT 1)")
35+
private Book prevBook;
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public String getTitle() {
46+
return title;
47+
}
48+
49+
public void setTitle(String title) {
50+
this.title = title;
51+
}
52+
53+
public String getIsbn() {
54+
return isbn;
55+
}
56+
57+
public void setIsbn(String isbn) {
58+
this.isbn = isbn;
59+
}
60+
61+
public Author getAuthor() {
62+
return author;
63+
}
64+
65+
public void setAuthor(Author author) {
66+
this.author = author;
67+
}
68+
69+
public int getPrice() {
70+
return price;
71+
}
72+
73+
public void setPrice(int price) {
74+
this.price = price;
75+
}
76+
77+
public Book getPrevBook() {
78+
return prevBook;
79+
}
80+
81+
public void setPrevBook(Book prevBook) {
82+
this.prevBook = prevBook;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return "Book{" + "id=" + id + ", title=" + title
88+
+ ", isbn=" + isbn + ", price=" + price + '}';
89+
}
90+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Repository
9+
public interface BookRepository extends JpaRepository<Book, Long> {
10+
11+
@Transactional(readOnly = true)
12+
Book findByTitle(String title);
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.repository.BookRepository;
4+
import com.bookstore.entity.Book;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Service
9+
public class BookstoreService {
10+
11+
private final BookRepository bookRepository;
12+
13+
public BookstoreService(BookRepository bookRepository) {
14+
15+
this.bookRepository = bookRepository;
16+
}
17+
18+
@Transactional(readOnly = true)
19+
public void fetchBooks() {
20+
21+
Book book = bookRepository.findById(4L).orElseThrow();
22+
Book prevBook = book.getPrevBook();
23+
24+
System.out.println("Fetched book with id 4: " + book);
25+
System.out.println("Fetched book with next smallest price: " + prevBook);
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNotExist=true
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
9+
10+
spring.jpa.open-in-view=false
11+
12+
spring.datasource.initialization-mode=always
13+
spring.datasource.platform=mysql
14+
15+
logging.level.org.hibernate.type.descriptor.sql=TRACE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
insert into author (age, name, genre, id) values (23, "Mark Janel", "Anthology", 1);
2+
insert into author (age, name, genre, id) values (43, "Olivia Goy", "Horror", 2);
3+
insert into author (age, name, genre, id) values (51, "Quartis Young", "Anthology", 3);
4+
insert into author (age, name, genre, id) values (34, "Joana Nimar", "History", 4);
5+
insert into book (isbn, title, price, author_id, id) values ("001-JN", "A History of Ancient Prague", 23, 4, 1);
6+
insert into book (isbn, title, price, author_id, id) values ("002-JN", "A People's History", 34, 4, 2);
7+
insert into book (isbn, title, price, author_id, id) values ("001-MJ", "The Beatles Anthology", 55, 1, 3);
8+
insert into book (isbn, title, price, author_id, id) values ("002-MJ", "Anthology Of '99", 44, 1, 4);
9+
insert into book (isbn, title, price, author_id, id) values ("001-OG", "Carrie", 33, 2, 5);
10+
insert into book (isbn, title, price, author_id, id) values ("002-OG", "Last Day", 25, 2, 6);
11+
insert into book (isbn, title, price, author_id, id) values ("003-JN", "History Today", 41, 4, 7);
12+
insert into book (isbn, title, price, author_id, id) values ("003-MJ", "Anthology Of A Game", 21, 1, 8);

0 commit comments

Comments
 (0)