The author field holds an Author object. At Many To One says, many books point to one author. JPA stores it as an author id column. And our constructor now takes the author too.
Book.javajava
@Entitypublic class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private double price; @ManyToOne private Author author; protected Book() {} public Book(String title, double price, Author author) { this.title = title; this.price = price; this.author = author; } // getters follow, including getAuthor()}
The database starts with three authors, and five books. Show SQL is on. The runner loads book one, and prints its author's name. Run it. Hibernate created the author table, and the book table, with an author id column, and a foreign key. Loading the book also joined the author table, so the name is ready.
RelDemo.javajava
@Componentpublic class RelDemo implements CommandLineRunner { private final AuthorRepository authors; private final BookRepository books; public RelDemo(AuthorRepository a, BookRepository b) { authors = a; books = b; } @Override public void run(String... args) { Book dune = books.findById(1L).orElseThrow(); System.out.println(dune.getTitle() + " by " + dune.getAuthor().getName()); }}
Output
Hibernate: create table author (id bigint generated by default as identity, name
varchar(255), primary key (id))
Hibernate: create table book (price float(53) not null, author_id bigint, id bigint
generated by default as identity, title varchar(255), primary key (id))
Hibernate: alter table if exists book add constraint FKklnrv3weler2ftkweewlky958 foreign
key (author_id) references author
Hibernate: select b1_0.id,a1_0.id,a1_0.name,b1_0.price,b1_0.title from book b1_0 left
join author a1_0 on a1_0.id=b1_0.author_id where b1_0.id=?
Dune by Frank Herbert
Also uses Author.java
@Entitypublic class Author { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "author") private List<Book> books = new ArrayList<>(); protected Author() {} public Author(String name) { this.name = name; } public Long getId() { return id; } public String getName() { return name; } public List<Book> getBooks() { return books; }}
Also uses Book.java
@Entitypublic class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private double price; @ManyToOne private Author author; protected Book() {} public Book(String title, double price, Author author) { this.title = title; this.price = price; this.author = author; } public Long getId() { return id; } public String getTitle() { return title; } public double getPrice() { return price; } public Author getAuthor() { return author; }}
Also uses AuthorRepository.java
public interface AuthorRepository extends JpaRepository<Author, Long> {}
Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {}
Also uses data.sql
INSERT INTO author (name) VALUES ('Frank Herbert'), ('J.R.R. Tolkien'), ('Madeline Miller');INSERT INTO book (title, price, author_id) VALUES ('Dune', 9.99, 1), ('Children of Dune', 8.49, 1), ('The Hobbit', 8.99, 2), ('Circe', 10.99, 3), ('Song of Achilles', 9.49, 3);