Skip to content

Commit 055b8b2

Browse files
committed
JPA HSQL One to one
1 parent e55d60b commit 055b8b2

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-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-One Foreign Key Example</name>
10+
<description>JPA One-To-One Foreign Key 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+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<java.version>1.7</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-data-jpa</artifactId>
27+
</dependency>
28+
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: 41 additions & 0 deletions
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.saveAll(books);
35+
36+
// fetch all books
37+
for (Book book : bookRepository.findAll()) {
38+
logger.info(book.toString());
39+
}
40+
}
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
@OneToOne(cascade = CascadeType.ALL)
14+
@JoinColumn(name = "book_detail_id")
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+
}
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 BookDetail getBookDetail() {
47+
return bookDetail;
48+
}
49+
50+
public void setBookDetail(BookDetail bookDetail) {
51+
this.bookDetail = bookDetail;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return String.format(
57+
"Book[id=%d, name='%s', number of pages='%d']",
58+
id, name, bookDetail.getNumberOfPages());
59+
}
60+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
@Table(name = "book_detail")
7+
public class BookDetail {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Integer id;
11+
12+
@Column(name = "number_of_pages")
13+
private Integer numberOfPages;
14+
15+
@OneToOne(mappedBy = "bookDetail")
16+
private Book book;
17+
18+
public BookDetail(){
19+
20+
}
21+
22+
public BookDetail(Integer numberOfPages){
23+
this.numberOfPages = numberOfPages;
24+
}
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public Integer getNumberOfPages() {
35+
return numberOfPages;
36+
}
37+
38+
public void setNumberOfPages(Integer numberOfPages) {
39+
this.numberOfPages = numberOfPages;
40+
}
41+
42+
public Book getBook() {
43+
return book;
44+
}
45+
46+
public void setBook(Book book) {
47+
this.book = book;
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
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)