One-to-One Relationship in Spring Boot Data JPA
For Explanation watch video :
Directory Structure::
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>SpringBootOneToOne</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootOneToOne</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
SpringBootOneToOneApplication
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootOneToOneApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootOneToOneApplication.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 javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
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;
@OneToOne
@JoinColumn(name = "bidFk")
private Book bob;
}
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 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 {
//create Bobs
Book bk1 = new Book(50,"DS");
Book bk2 = new Book(60,"SB");
Book bk3 = new Book(70,"HB");
//save objs
brepo.save(bk1);
brepo.save(bk2);
brepo.save(bk3);
//create std obj
Student st1 = new Student(101,"SAM",bk1);
Student st2 = new Student(102,"JHON",bk2);
Student st3 = new Student(103,"Angela",bk3);
//save obj
srepo.save(st1);
srepo.save(st2);
srepo.save(st3);
}
}
Comments
Post a Comment