File tree Expand file tree Collapse file tree 8 files changed +208
-0
lines changed
springboot-examples/springdatarest-mysql-one-to-many
java/com/hellokoding/restfulapi Expand file tree Collapse file tree 8 files changed +208
-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.onetomany</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+
5+ @ Entity
6+ public class Book {
7+ @ Id
8+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
9+ private int id ;
10+
11+ private String title ;
12+
13+ private String description ;
14+
15+ @ ManyToOne
16+ @ JoinColumn (name = "book_category_id" )
17+ private BookCategory bookCategory ;
18+
19+ public Book () {
20+
21+ }
22+
23+ public int getId () {
24+ return id ;
25+ }
26+
27+ public void setId (int id ) {
28+ this .id = id ;
29+ }
30+
31+ public String getTitle () {
32+ return title ;
33+ }
34+
35+ public void setTitle (String title ) {
36+ this .title = title ;
37+ }
38+
39+ public String getDescription () {
40+ return description ;
41+ }
42+
43+ public void setDescription (String description ) {
44+ this .description = description ;
45+ }
46+
47+ public BookCategory getBookCategory () {
48+ return bookCategory ;
49+ }
50+
51+ public void setBookCategory (BookCategory bookCategory ) {
52+ this .bookCategory = bookCategory ;
53+ }
54+ }
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+ @ Table (name = "book_category" )
8+ public class BookCategory {
9+ @ Id
10+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
11+ private int id ;
12+
13+ private String name ;
14+
15+ @ OneToMany (mappedBy = "bookCategory" , cascade = CascadeType .ALL )
16+ private Set <Book > books ;
17+
18+ public BookCategory (){
19+
20+ }
21+
22+ public BookCategory (String name ) {
23+ this .name = name ;
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 <Book > getBooks () {
43+ return books ;
44+ }
45+
46+ public void setBooks (Set <Book > books ) {
47+ this .books = books ;
48+ }
49+
50+ @ Override
51+ public String toString () {
52+ String result = String .format (
53+ "Category[id=%d, name='%s']%n" ,
54+ id , name );
55+ if (books != null ) {
56+ for (Book book : books ) {
57+ result += String .format (
58+ "Book[id=%d, title='%s']%n" ,
59+ book .getId (), book .getTitle ());
60+ }
61+ }
62+
63+ return result ;
64+ }
65+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .restfulapi .repository ;
2+
3+ import com .hellokoding .restfulapi .model .BookCategory ;
4+ import org .springframework .data .jpa .repository .JpaRepository ;
5+
6+ public interface BookCategoryRepository extends JpaRepository <BookCategory , 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+ 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
You can’t perform that action at this time.
0 commit comments