Skip to content

Commit 658dfed

Browse files
committed
JPA
1 parent fcf7b1f commit 658dfed

8 files changed

Lines changed: 123 additions & 175 deletions

File tree

‎jpa-hibernate-examples/jpa-hibernate-many-to-many-hsql/pom.xml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
<artifactId>spring-boot-starter-data-jpa</artifactId>
2626
</dependency>
2727
<dependency>
28-
<groupId>org.hsqldb</groupId>
29-
<artifactId>hsqldb</artifactId>
28+
<groupId>mysql</groupId>
29+
<artifactId>mysql-connector-java</artifactId>
3030
<scope>runtime</scope>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
</dependency>
3236
</dependencies>
3337

3438
<build>

‎jpa-hibernate-examples/jpa-hibernate-many-to-many-hsql/src/main/java/com/hellokoding/jpa/Application.java‎

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.hellokoding.jpa;
2+
3+
import com.hellokoding.jpa.model.Book;
4+
import com.hellokoding.jpa.model.BookPublisher;
5+
import com.hellokoding.jpa.model.Publisher;
6+
import com.hellokoding.jpa.repository.BookPublisherRepository;
7+
import com.hellokoding.jpa.repository.BookRepository;
8+
import com.hellokoding.jpa.repository.PublisherRepository;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.boot.CommandLineRunner;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
14+
import java.util.Arrays;
15+
import java.util.Date;
16+
17+
@RequiredArgsConstructor
18+
@SpringBootApplication
19+
public class JpaApplication implements CommandLineRunner {
20+
private final BookRepository bookRepository;
21+
private final PublisherRepository publisherRepository;
22+
private final BookPublisherRepository bookPublisherRepository;
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(JpaApplication.class, args);
26+
}
27+
28+
@Override
29+
public void run(String... args) {
30+
Book b1 = new Book("Spring Boot");
31+
Book b2 = new Book("Spring Data JPA");
32+
bookRepository.saveAll(Arrays.asList(b1, b2));
33+
34+
Publisher p1 = new Publisher("HelloKoding 1");
35+
Publisher p2 = new Publisher("HelloKoding 2");
36+
publisherRepository.saveAll(Arrays.asList(p1, p2));
37+
38+
BookPublisher bp1 = new BookPublisher(b1, p1, new Date());
39+
BookPublisher bp2 = new BookPublisher(b1, p2, new Date());
40+
BookPublisher bp3 = new BookPublisher(b2, p1, new Date());
41+
BookPublisher bp4 = new BookPublisher(b2, p2, new Date());
42+
bookPublisherRepository.saveAll(Arrays.asList(bp1, bp2, bp3, bp4));
43+
}
44+
}
Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,20 @@
11
package com.hellokoding.jpa.model;
22

3+
import lombok.Data;
4+
35
import javax.persistence.*;
4-
import java.util.Set;
56

7+
@Data
68
@Entity
7-
public class Book{
9+
public class Book {
810
@Id
911
@GeneratedValue(strategy = GenerationType.IDENTITY)
1012
private int id;
1113

14+
@Column(name = "name")
1215
private String name;
1316

14-
@ManyToMany(cascade = CascadeType.ALL)
15-
@JoinTable(name = "book_publisher", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "publisher_id", referencedColumnName = "id"))
16-
private Set<Publisher> publishers;
17-
18-
public Book() {
19-
20-
}
21-
2217
public Book(String name) {
2318
this.name = name;
2419
}
25-
26-
public Book(String name, Set<Publisher> publishers){
27-
this.name = name;
28-
this.publishers = publishers;
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 Set<Publisher> getPublishers() {
48-
return publishers;
49-
}
50-
51-
public void setPublishers(Set<Publisher> publishers) {
52-
this.publishers = publishers;
53-
}
54-
55-
@Override
56-
public String toString() {
57-
String result = String.format(
58-
"Book [id=%d, name='%s']%n",
59-
id, name);
60-
if (publishers != null) {
61-
for(Publisher publisher : publishers) {
62-
result += String.format(
63-
"Publisher[id=%d, name='%s']%n",
64-
publisher.getId(), publisher.getName());
65-
}
66-
}
67-
68-
return result;
69-
}
7020
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import lombok.Setter;
6+
7+
import javax.persistence.*;
8+
import java.util.Date;
9+
import java.util.Objects;
10+
11+
@Getter @Setter @NoArgsConstructor
12+
@Entity
13+
public class BookPublisher {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private int id;
17+
18+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
19+
@JoinColumn(name = "book_id")
20+
private Book book;
21+
22+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
23+
@JoinColumn(name = "publisher_id")
24+
private Publisher publisher;
25+
26+
private Date publishedDate;
27+
28+
public BookPublisher(Book book, Publisher publisher, Date publishedDate) {
29+
this.book = book;
30+
this.publisher = publisher;
31+
this.publishedDate = publishedDate;
32+
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (this == o) return true;
37+
if (!(o instanceof BookPublisher)) return false;
38+
BookPublisher that = (BookPublisher) o;
39+
return Objects.equals(book.getName(), that.book.getName()) &&
40+
Objects.equals(publisher.getName(), that.publisher.getName()) &&
41+
Objects.equals(publishedDate, that.publishedDate);
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(book.getName(), publisher.getName(), publishedDate);
47+
}
48+
}
Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.hellokoding.jpa.model;
22

3+
import lombok.Data;
4+
35
import javax.persistence.*;
4-
import java.util.Set;
56

7+
@Data
68
@Entity
79
public class Publisher {
810
@Id
@@ -11,59 +13,7 @@ public class Publisher {
1113

1214
private String name;
1315

14-
@ManyToMany(mappedBy = "publishers")
15-
private Set<Book> books;
16-
17-
public Publisher(){
18-
19-
}
20-
21-
public Publisher(String name){
22-
this.name = name;
23-
}
24-
25-
public Publisher(String name, Set<Book> books){
26-
this.name = name;
27-
this.books = books;
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) {
16+
public Publisher(String name) {
4317
this.name = name;
4418
}
45-
46-
public Set<Book> getBooks() {
47-
return books;
48-
}
49-
50-
public void setBooks(Set<Book> books) {
51-
this.books = books;
52-
}
53-
54-
@Override
55-
public String toString() {
56-
String result = String.format(
57-
"Publisher [id=%d, name='%s']%n",
58-
id, name);
59-
if (books != null) {
60-
for(Book book : books) {
61-
result += String.format(
62-
"Book[id=%d, name='%s']%n",
63-
book.getId(), book.getName());
64-
}
65-
}
66-
67-
return result;
68-
}
6919
}
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.BookPublisher;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface BookPublisherRepository extends JpaRepository<BookPublisher, Integer> {
7+
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
spring.jpa.hibernate.ddl-auto=create-drop
2-
spring.jpa.show-sql=true
1+
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
2+
spring.datasource.username=root
3+
spring.datasource.password=hellokoding
4+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5+
6+
spring.jpa.hibernate.ddl-auto=create
7+
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
8+
spring.jpa.generate-ddl=true
9+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)