Skip to content

Commit 171881e

Browse files
committed
JPA HSQL Many to many extra columns
1 parent c1de362 commit 171881e

File tree

9 files changed

+269
-0
lines changed

9 files changed

+269
-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 Many-To-Many with Extra Columns Example</name>
10+
<description>JPA Many-To-Many with Extra Columns 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+
<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>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.BookRepository;
7+
import com.hellokoding.jpa.repository.PublisherRepository;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.CommandLineRunner;
12+
import org.springframework.boot.SpringApplication;
13+
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
15+
import javax.transaction.Transactional;
16+
import java.util.Date;
17+
18+
@SpringBootApplication
19+
public class Application implements CommandLineRunner {
20+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
21+
22+
@Autowired
23+
private BookRepository bookRepository;
24+
25+
@Autowired
26+
private PublisherRepository publisherRepository;
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(Application.class, args);
30+
}
31+
32+
@Override
33+
@Transactional
34+
public void run(String... strings) throws Exception {
35+
// create new
36+
Book bookA = new Book("Book A");
37+
Publisher publisherA = new Publisher("Publisher A");
38+
39+
BookPublisher bookPublisher = new BookPublisher();
40+
bookPublisher.setBook(bookA);
41+
bookPublisher.setPublisher(publisherA);
42+
bookPublisher.setPublishedDate(new Date());
43+
44+
bookA.getBookPublishers().add(bookPublisher);
45+
46+
publisherRepository.save(publisherA);
47+
bookRepository.save(bookA);
48+
49+
// test
50+
System.out.println(bookA.getBookPublishers().size());
51+
52+
// update
53+
bookA.getBookPublishers().remove(bookPublisher);
54+
bookRepository.save(bookA);
55+
56+
// test
57+
System.out.println(bookA.getBookPublishers().size());
58+
}
59+
}
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+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
@Entity
8+
public class Book{
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private int id;
12+
13+
private String name;
14+
15+
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
16+
private Set<BookPublisher> bookPublishers;
17+
18+
public Book() {
19+
}
20+
21+
public Book(String name) {
22+
this.name = name;
23+
bookPublishers = new HashSet<>();
24+
}
25+
26+
public int getId() {
27+
return id;
28+
}
29+
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Set<BookPublisher> getBookPublishers() {
43+
return bookPublishers;
44+
}
45+
46+
public void setBookPublishers(Set<BookPublisher> bookPublishers) {
47+
this.bookPublishers = bookPublishers;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
import java.util.Date;
6+
7+
@Entity
8+
@Table(name = "book_publisher")
9+
public class BookPublisher implements Serializable{
10+
@Id
11+
@ManyToOne
12+
@JoinColumn(name = "book_id")
13+
private Book book;
14+
15+
@Id
16+
@ManyToOne
17+
@JoinColumn(name = "publisher_id")
18+
private Publisher publisher;
19+
20+
@Column(name = "published_date")
21+
private Date publishedDate;
22+
23+
public Book getBook() {
24+
return book;
25+
}
26+
27+
public void setBook(Book book) {
28+
this.book = book;
29+
}
30+
31+
32+
public Publisher getPublisher() {
33+
return publisher;
34+
}
35+
36+
public void setPublisher(Publisher publisher) {
37+
this.publisher = publisher;
38+
}
39+
40+
public Date getPublishedDate() {
41+
return publishedDate;
42+
}
43+
44+
public void setPublishedDate(Date publishedDate) {
45+
this.publishedDate = publishedDate;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hellokoding.jpa.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
public class Publisher {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String name;
13+
14+
@OneToMany(mappedBy = "publisher")
15+
private Set<BookPublisher> bookPublishers;
16+
17+
public Publisher(){
18+
19+
}
20+
21+
public Publisher(String name){
22+
this.name = name;
23+
}
24+
25+
public int getId() {
26+
return id;
27+
}
28+
29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public Set<BookPublisher> getBookPublishers() {
42+
return bookPublishers;
43+
}
44+
45+
public void setBookPublishers(Set<BookPublisher> bookPublishers) {
46+
this.bookPublishers = bookPublishers;
47+
}
48+
}
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+
}
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.Publisher;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface PublisherRepository extends JpaRepository<Publisher, 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
2+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)