Skip to content

Commit bdc4913

Browse files
committed
Composite PK via Embeddable
1 parent 910345c commit bdc4913

File tree

10 files changed

+462
-0
lines changed

10 files changed

+462
-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>HibernateSpringBootCompositeKeyEmbeddable</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootCompositeKeyEmbeddable</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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
26+
System.out.println("\nAdd author with books ...");
27+
bookstoreService.addAuthorWithBooks();
28+
29+
System.out.println("\nFetch author by name ...");
30+
bookstoreService.fetchAuthorByName();
31+
32+
System.out.println("\nRemove a book of an author ...");
33+
bookstoreService.removeBookOfAuthor();
34+
35+
System.out.println("\nRemove author (and books via cascade) ...");
36+
bookstoreService.removeAuthor();
37+
};
38+
}
39+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import javax.persistence.CascadeType;
8+
import javax.persistence.EmbeddedId;
9+
import javax.persistence.Entity;
10+
import javax.persistence.OneToMany;
11+
12+
@Entity
13+
public class Author implements Serializable {
14+
15+
private static final long serialVersionUID = 1L;
16+
17+
@EmbeddedId
18+
private AuthorId id;
19+
20+
private String genre;
21+
22+
@OneToMany(cascade = CascadeType.ALL,
23+
mappedBy = "author", orphanRemoval = true)
24+
private List<Book> books = new ArrayList<>();
25+
26+
public void addBook(Book book) {
27+
this.books.add(book);
28+
book.setAuthor(this);
29+
}
30+
31+
public void removeBook(Book book) {
32+
book.setAuthor(null);
33+
this.books.remove(book);
34+
}
35+
36+
public void removeBooks() {
37+
Iterator<Book> iterator = this.books.iterator();
38+
39+
while (iterator.hasNext()) {
40+
Book book = iterator.next();
41+
42+
book.setAuthor(null);
43+
iterator.remove();
44+
}
45+
}
46+
47+
public AuthorId getId() {
48+
return id;
49+
}
50+
51+
public void setId(AuthorId id) {
52+
this.id = id;
53+
}
54+
55+
public String getGenre() {
56+
return genre;
57+
}
58+
59+
public void setGenre(String genre) {
60+
this.genre = genre;
61+
}
62+
63+
public List<Book> getBooks() {
64+
return books;
65+
}
66+
67+
public void setBooks(List<Book> books) {
68+
this.books = books;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Author{" + "id=" + id + ", genre=" + genre + '}';
74+
}
75+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.bookstore.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
import javax.persistence.Column;
6+
import javax.persistence.Embeddable;
7+
8+
@Embeddable
9+
public class AuthorId implements Serializable {
10+
11+
private static final long serialVersionUID = 1L;
12+
13+
@Column(name = "name")
14+
private String name;
15+
16+
@Column(name = "age")
17+
private int age;
18+
19+
public AuthorId() {
20+
}
21+
22+
public AuthorId(String name, int age) {
23+
this.name = name;
24+
this.age = age;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public int getAge() {
32+
return age;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
int hash = 3;
38+
hash = 23 * hash + Objects.hashCode(this.name);
39+
hash = 23 * hash + this.age;
40+
return hash;
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
46+
if (this == obj) {
47+
return true;
48+
}
49+
50+
if (obj == null) {
51+
return false;
52+
}
53+
54+
if (getClass() != obj.getClass()) {
55+
return false;
56+
}
57+
58+
final AuthorId other = (AuthorId) obj;
59+
if (this.age != other.age) {
60+
return false;
61+
}
62+
63+
if (!Objects.equals(this.name, other.name)) {
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "AuthorId{" + "name=" + name + ", age=" + age + '}';
73+
}
74+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.JoinColumns;
11+
import javax.persistence.ManyToOne;
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+
25+
@ManyToOne(fetch = FetchType.LAZY)
26+
@JoinColumns({
27+
@JoinColumn(
28+
name = "name",
29+
referencedColumnName = "name"),
30+
@JoinColumn(
31+
name = "age",
32+
referencedColumnName = "age")
33+
})
34+
private Author author;
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public void setId(Long id) {
41+
this.id = id;
42+
}
43+
44+
public String getTitle() {
45+
return title;
46+
}
47+
48+
public void setTitle(String title) {
49+
this.title = title;
50+
}
51+
52+
public String getIsbn() {
53+
return isbn;
54+
}
55+
56+
public void setIsbn(String isbn) {
57+
this.isbn = isbn;
58+
}
59+
60+
public Author getAuthor() {
61+
return author;
62+
}
63+
64+
public void setAuthor(Author author) {
65+
this.author = author;
66+
}
67+
68+
@Override
69+
public boolean equals(Object obj) {
70+
71+
if (this == obj) {
72+
return true;
73+
}
74+
75+
if (getClass() != obj.getClass()) {
76+
return false;
77+
}
78+
79+
return id != null && id.equals(((Book) obj).id);
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
return 2021;
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return "Book{" + "id=" + id + ", title=" + title + ", isbn=" + isbn + '}';
90+
}
91+
92+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import com.bookstore.entity.AuthorId;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Repository
10+
public interface AuthorRepository extends JpaRepository<Author, AuthorId> {
11+
12+
@Query("SELECT a FROM Author a "
13+
+ "JOIN FETCH a.books WHERE a.id = ?1")
14+
public Author fetchWithBooks(AuthorId id);
15+
16+
@Query("SELECT a FROM Author a WHERE a.id.name = ?1")
17+
public Author fetchByName(String name);
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
7+
@Repository
8+
public interface BookRepository extends JpaRepository<Book, Long> {
9+
}

0 commit comments

Comments
 (0)