Skip to content

Commit d40cb97

Browse files
committed
Spring data rest many to many
1 parent 8c3c471 commit d40cb97

File tree

8 files changed

+185
-0
lines changed

8 files changed

+185
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
*.iml
3+
.idea
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
6+
<groupId>com.hellokoding.springdatarest</groupId>
7+
<artifactId>restfull.maytomany</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>2.1.4.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-rest</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-jpa</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>mysql</groupId>
33+
<artifactId>mysql-connector-java</artifactId>
34+
<scope>runtime</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.hellokoding.restfulapi;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hellokoding.restfulapi.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
public class Author {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String name;
13+
14+
@ManyToMany(mappedBy = "authors")
15+
private Set<Book> books;
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public Set<Book> getBooks() {
34+
return books;
35+
}
36+
37+
public void setBooks(Set<Book> books) {
38+
this.books = books;
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.hellokoding.restfulapi.model;
2+
3+
import javax.persistence.*;
4+
import java.util.Set;
5+
6+
@Entity
7+
public class Book{
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private int id;
11+
12+
private String title;
13+
14+
private String description;
15+
16+
@ManyToMany(cascade = CascadeType.ALL)
17+
@JoinTable(name = "book_author", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
18+
private Set<Author> authors;
19+
20+
public Book() {
21+
22+
}
23+
24+
public int getId() {
25+
return id;
26+
}
27+
28+
public void setId(int id) {
29+
this.id = id;
30+
}
31+
32+
public String getTitle() {
33+
return title;
34+
}
35+
36+
public void setTitle(String title) {
37+
this.title = title;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public void setDescription(String description) {
45+
this.description = description;
46+
}
47+
48+
public Set<Author> getAuthors() {
49+
return authors;
50+
}
51+
52+
public void setAuthors(Set<Author> authors) {
53+
this.authors = authors;
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.hellokoding.restfulapi.repository;
2+
3+
import com.hellokoding.restfulapi.model.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface AuthorRepository extends JpaRepository<Author, Integer>{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.hellokoding.restfulapi.repository;
2+
3+
import com.hellokoding.restfulapi.model.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface BookRepository extends JpaRepository<Book, Integer>{
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.datasource.url=jdbc:mysql://localhost/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.datasource.initialization-mode=always
7+
spring.jpa.hibernate.ddl-auto=create-drop
8+
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
9+
spring.jpa.generate-ddl=true
10+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)