Skip to content

Commit b45efad

Browse files
committed
added Author Entity
1 parent 7d006fc commit b45efad

File tree

8 files changed

+126
-11
lines changed

8 files changed

+126
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.model;
2+
3+
import org.hibernate.annotations.BatchSize;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "authors")
14+
@BatchSize(size = 10)
15+
public class Author {
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
Long id;
19+
@Column(name = "full_name")
20+
String name;
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+
}

src/main/java/com/example/model/Book.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public class Book {
1010
private Long id;
1111
private String title;
1212
private int pages;
13-
1413
private int year;
1514

15+
@ManyToOne(fetch = FetchType.LAZY)
16+
@JoinColumn(name = "author_id")
17+
private Author author;
18+
1619
public Book(String title, int pages) {
1720
this.title = title;
1821
this.pages = pages;
@@ -52,4 +55,12 @@ public int getYear() {
5255
public void setYear(int year) {
5356
this.year = year;
5457
}
58+
59+
public Author getAuthor() {
60+
return author;
61+
}
62+
63+
public void setAuthor(Author author) {
64+
this.author = author;
65+
}
5566
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.repository;
2+
3+
import com.example.model.Author;
4+
import io.micronaut.data.annotation.Repository;
5+
import io.micronaut.data.repository.CrudRepository;
6+
7+
@Repository
8+
public interface AuthorRepository extends CrudRepository<Author, Long> {
9+
}

src/main/java/com/example/repository/BookRepository.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
import com.example.model.Book;
44
import io.micronaut.context.annotation.Executable;
55
import io.micronaut.data.annotation.*;
6+
import io.micronaut.data.jpa.annotation.EntityGraph;
7+
import io.micronaut.data.jpa.repository.JpaSpecificationExecutor;
68
import io.micronaut.data.repository.CrudRepository;
79

8-
@Repository //
9-
interface BookRepository extends CrudRepository<Book, Long> {
10-
11-
@Executable
12-
Book find(String title);
13-
10+
import java.util.List;
1411

12+
@Repository
13+
public interface BookRepository extends CrudRepository<Book, Long>, JpaSpecificationExecutor<Book> {
1514

15+
@Executable
16+
Book findByTitle(String title);
1617

18+
@Join(value = "author", type = Join.Type.FETCH) //
19+
List<Book> list();
1720

21+
@EntityGraph(attributePaths = {"author", "title"}) //
22+
List<Book> findAll();
1823
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.service;
2+
3+
import com.example.repository.AuthorRepository;
4+
import io.micronaut.context.annotation.Primary;
5+
import jakarta.inject.Inject;
6+
import jakarta.inject.Singleton;
7+
8+
@Primary
9+
@Singleton
10+
public class AuthorService {
11+
12+
@Inject
13+
AuthorRepository repository;
14+
15+
16+
public void getAll(){
17+
repository.findAll();
18+
}
19+
}

src/main/java/com/example/service/BookService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.example.service;
22

3-
import com.example.BookRepository;
43
import com.example.model.Book;
4+
import com.example.repository.BookRepository;
55
import io.micronaut.context.annotation.Primary;
66
import jakarta.inject.Inject;
77
import jakarta.inject.Singleton;
@@ -30,7 +30,7 @@ public void initDataBase(){
3030
@Transactional
3131
public List<Book> findAll(){
3232

33-
return (List<Book>) repository.findAll();
33+
return repository.findAll();
3434
}
3535

3636
@Transactional

src/main/resources/application.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ datasources:
1414
username: 'sa'
1515
password: ''
1616
driverClassName: 'org.h2.Driver'
17+
1718
jpa:
1819
default:
19-
packages-to-scan:
20-
- 'com.example'
20+
entity-scan:
21+
packages: 'com.example.model'
2122
properties:
2223
hibernate:
2324
hbm2ddl:

src/main/resources/db/liquibase-changelog.xml

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
77
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
8+
89
<changeSet id="001" author="ivan_tsiupa">
910
<sql>
1011
CREATE TABLE IF NOT EXISTS books
@@ -47,4 +48,35 @@
4748
UPDATE books SET year = 2007 WHERE title = 'Deathly Hallows';
4849
</sql>
4950
</changeSet>
51+
52+
<changeSet id="005" author="ivan_tsiupa">
53+
<sql>
54+
CREATE TABLE IF NOT EXISTS authors (
55+
id BIGSERIAL NOT NULL,
56+
full_name TEXT NOT NULL,
57+
PRIMARY KEY (id)
58+
);
59+
</sql>
60+
</changeSet>
61+
62+
<changeSet id="006" author="ivan_tsiupa">
63+
<sql>
64+
INSERT INTO authors(full_name) VALUES('J. K. Rowling');
65+
</sql>
66+
</changeSet>
67+
68+
69+
<changeSet id="007" author="ivan_tsiupa">
70+
<sql>
71+
ALTER TABLE books ADD author_id BIGINT constraint author_id_constraint references authors;
72+
</sql>
73+
</changeSet>
74+
75+
<changeSet id="008" author="ivan_tsiupa">
76+
<sql>
77+
UPDATE books SET author_id = 1;
78+
</sql>
79+
</changeSet>
80+
81+
5082
</databaseChangeLog>

0 commit comments

Comments
 (0)