Skip to content

Commit c1de362

Browse files
committed
JPA HSQL One to one shared primary key
1 parent 055b8b2 commit c1de362

File tree

7 files changed

+201
-0
lines changed

7 files changed

+201
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 One Shared Primary Key Example</name>
10+
<description>JPA One to One Shared Primary Key Example</description>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.3.5.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+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.hellokoding.jpa;
2+
3+
import com.hellokoding.jpa.model.Book;
4+
import com.hellokoding.jpa.model.BookDetail;
5+
import com.hellokoding.jpa.repository.BookRepository;
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 java.util.ArrayList;
14+
import java.util.List;
15+
16+
@SpringBootApplication
17+
public class Application implements CommandLineRunner {
18+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
19+
20+
@Autowired
21+
private BookRepository bookRepository;
22+
23+
public static void main(String[] args) {
24+
SpringApplication.run(Application.class, args);
25+
}
26+
27+
@Override
28+
public void run(String... strings) throws Exception {
29+
// save a couple of books
30+
List<Book> books = new ArrayList<>();
31+
books.add(new Book("Book A", new BookDetail(49)));
32+
books.add(new Book("Book B", new BookDetail(59)));
33+
books.add(new Book("Book C", new BookDetail(69)));
34+
bookRepository.save(books);
35+
36+
// fetch all books
37+
for (Book book : bookRepository.findAll()) {
38+
logger.info(book.toString());
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
public class Book implements Serializable{
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String name;
13+
14+
@OneToOne(cascade = CascadeType.ALL, mappedBy = "book")
15+
private BookDetail bookDetail;
16+
17+
public Book(){
18+
19+
}
20+
21+
public Book(String name){
22+
this.name = name;
23+
}
24+
25+
public Book(String name, BookDetail bookDetail){
26+
this.name = name;
27+
this.bookDetail = bookDetail;
28+
this.bookDetail.setBook(this);
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 BookDetail getBookDetail() {
48+
return bookDetail;
49+
}
50+
51+
public void setBookDetail(BookDetail bookDetail) {
52+
this.bookDetail = bookDetail;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return String.format(
58+
"Book[id=%d, name='%s', number of pages='%d']",
59+
id, name, bookDetail.getNumberOfPages());
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
@Table(name = "book_detail")
8+
public class BookDetail implements Serializable{
9+
@Column(name = "number_of_pages")
10+
private Integer numberOfPages;
11+
12+
@Id
13+
@OneToOne
14+
@JoinColumn(name = "book_id")
15+
private Book book;
16+
17+
public BookDetail(){
18+
19+
}
20+
21+
public BookDetail(Integer numberOfPages){
22+
this.numberOfPages = numberOfPages;
23+
}
24+
25+
public Integer getNumberOfPages() {
26+
return numberOfPages;
27+
}
28+
29+
public void setNumberOfPages(Integer numberOfPages) {
30+
this.numberOfPages = numberOfPages;
31+
}
32+
33+
public Book getBook() {
34+
return book;
35+
}
36+
37+
public void setBook(Book book) {
38+
this.book = book;
39+
}
40+
}
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+
}
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)