File tree Expand file tree Collapse file tree 10 files changed +250
-0
lines changed
springboot-examples/springdatarest-mysql-many-to-many-extra-columns
java/com/hellokoding/restfulapi Expand file tree Collapse file tree 10 files changed +250
-0
lines changed Original file line number Diff line number Diff line change 1+ target
2+ * .iml
3+ .idea
Original file line number Diff line number Diff line change 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.manytomany.extracolumns</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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .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 number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .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+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
12+ private int id ;
13+
14+ @ ManyToOne
15+ @ JoinColumn (name = "book_id" )
16+ private Book book ;
17+
18+ @ ManyToOne
19+ @ JoinColumn (name = "publisher_id" )
20+ private Publisher publisher ;
21+
22+ @ Column (name = "published_date" )
23+ private Date publishedDate ;
24+
25+ public int getId () {
26+ return id ;
27+ }
28+
29+ public void setId (int id ) {
30+ this .id = id ;
31+ }
32+
33+ public Book getBook () {
34+ return book ;
35+ }
36+
37+ public void setBook (Book book ) {
38+ this .book = book ;
39+ }
40+
41+
42+ public Publisher getPublisher () {
43+ return publisher ;
44+ }
45+
46+ public void setPublisher (Publisher publisher ) {
47+ this .publisher = publisher ;
48+ }
49+
50+ public Date getPublishedDate () {
51+ return publishedDate ;
52+ }
53+
54+ public void setPublishedDate (Date publishedDate ) {
55+ this .publishedDate = publishedDate ;
56+ }
57+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .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 number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .repository ;
2+
3+ import com .hellokoding .restfulapi .model .BookPublisher ;
4+ import org .springframework .data .jpa .repository .JpaRepository ;
5+
6+ public interface BookPublisherRepository extends JpaRepository <BookPublisher , Integer >{
7+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .repository ;
2+
3+ import com .hellokoding .restfulapi .model .Publisher ;
4+ import org .springframework .data .jpa .repository .JpaRepository ;
5+
6+ public interface PublisherRepository extends JpaRepository <Publisher , Integer >{
7+ }
Original file line number Diff line number Diff line change 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.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
You can’t perform that action at this time.
0 commit comments