One-to-Many Relationship in Spring Boot Data JPA
For Explanation watch video ::
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>SpringBootOneToMany</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootOneToMany</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#datsource
spring.datasource.url=jdbc:mysql://localhost:3306/new
spring.datasource.username=root
spring.datasource.password=root
#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
SpringBootOneToManyApplication
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootOneToManyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootOneToManyApplication.class, args);
}
}
Book
package com.app.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "bktab")
public class Book {
@Id
private Integer bid;
private String bname;
}
Student
package com.app.entity;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "stdtab")
public class Student {
@Id
private Integer sid;
private String sname;
@OneToMany
@JoinColumn(name = "sidFk")
private Set<Book> bobs;
}
BookRepository
package com.app.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.app.entity.Book;
public interface BookRepository extends JpaRepository<Book, Integer>{
}
StudentRepository
package com.app.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.app.entity.Student;
public interface StudentRepository extends JpaRepository<Student, Integer>{
}
TestRunner
package com.app.runner;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.app.entity.Book;
import com.app.entity.Student;
import com.app.repo.BookRepository;
import com.app.repo.StudentRepository;
@Component
public class TestRunner implements CommandLineRunner{
@Autowired
private BookRepository brepo;
@Autowired
private StudentRepository srepo;
@Override
public void run(String... args) throws Exception {
//book objs
Book bk1 = new Book(50,"DS");
Book bk2 = new Book(60,"SB");
//save books
brepo.save(bk1);
brepo.save(bk2);
//set
Set<Book> s1 = Set.of(bk1,bk2);
//stu obj
Student st1 = new Student(101,"SAM",s1);
//save stu obj
srepo.save(st1);
}
}
Comments
Post a Comment