in this article we will cover one to many relationship in spring boot.
employ has many Addresses in uni-direction relationships. JPA One To Many with Hibernate in Spring Boot is a way to map a one-to-many relationship between two entities in a database. The one-to-many relationship means that one row in a table can be associated with multiple rows in another table. JPA One To Many example with Hibernate and Spring Boot
To implement JPA One To Many with Hibernate in Spring Boot, you will need to use the following annotations:
@OneToMany– This annotation is used to declare a one-to-many relationship.@JoinColumn– This annotation is used to specify the foreign key column that is used to link the two tables.
In this tutorial, I will show you how to implement Spring JPA One-To-Many mapping with Hibernate in a Spring Boot CRUD example using annotation. You’ll know: JPA One To Many examples with Hibernate and Spring Boot
- How to configure Spring Data, JPA, and Hibernate to work with Database
- How to define Data Models and Repository interfaces for JPA One-To-Many relationship
- Way to use Spring JPA to interact with Database for One-To-Many association
- Way to create Spring Rest Controller to process HTTP requests
Employ Entity Class
package com.onetomany.demoonetomany.Employ;
import com.onetomany.demoonetomany.Address.Address;
import javax.persistence.*;
import java.util.List;
@Entity
public class Employ {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String name;
String status;
@OneToMany(cascade =CascadeType.ALL)
@JoinColumn
List<Address> addressList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
public Employ(String name, String status, List<Address> addressList) {
this.name = name;
this.status = status;
this.addressList = addressList;
}
public Employ(int id, String name, String status, List<Address> addressList) {
this.id = id;
this.name = name;
this.status = status;
this.addressList = addressList;
}
public Employ() {
}
}
In the Department entity, we have a One-to-Many relationship with Employee, specified by the @OneToMany annotation. The mappedBy attribute indicates that the relationship is mapped by the department field in the Employee entity.
In the Employee entity, we have a Many-to-One relationship with Department, specified by the @ManyToOne annotation. The @JoinColumn annotation specifies the foreign key column in the employees table that references the departments table.
Employ Repository Class
package com.onetomany.demoonetomany.Employ;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployRepository extends JpaRepository<Employ,Integer> {
}
Address Entity Class
package com.onetomany.demoonetomany.Address;
import com.onetomany.demoonetomany.Country.Country;
import javax.persistence.*;
import java.util.List;
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String addressName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn
List<Country> country;
public List<Country> getCountry() {
return country;
}
public void setCountry(List<Country> country) {
this.country = country;
}
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
}
Address Repository Class
package com.onetomany.demoonetomany.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface Addressrepository extends JpaRepository<Address,Integer> {
}
Controller Class
package com.onetomany.demoonetomany.API;
import com.onetomany.demoonetomany.Address.Address;
import com.onetomany.demoonetomany.Address.Addressrepository;
import com.onetomany.demoonetomany.Comment.Comment;
import com.onetomany.demoonetomany.Comment.CommentRepository;
import com.onetomany.demoonetomany.Country.Country;
import com.onetomany.demoonetomany.Country.CountryRepository;
import com.onetomany.demoonetomany.Employ.Employ;
import com.onetomany.demoonetomany.Employ.EmployRepository;
import com.onetomany.demoonetomany.Post.Post;
import com.onetomany.demoonetomany.Post.PostRepository;
import com.onetomany.demoonetomany.Request.PostRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
public class Controller {
@Autowired
EmployRepository employRepository;
@Autowired
Addressrepository addressrepository;
@Autowired
CountryRepository countryRepository;
@Autowired
PostRepository postRepository;
@Autowired
CommentRepository commentRepository;
@PostMapping("/add/employ")
public Employ addemploy(@RequestBody Employ employ){
List<Country> countryList=new ArrayList<>();
Country country=new Country();
country.setStatename("Punjab");
country.setCode(12340);
countryList.add(country);
List<Address> addressList=new ArrayList<>();
Address address=new Address();
address.setAddressName("sheikh umad kohna kasur");
address.setCountry(countryList);
addressList.add(address);
Employ employ1=new Employ();
employ1.setName("Muhammad Waseem haider");
employ1.setStatus("Mariage");
employ1.setAddressList(addressList);
return employRepository.save(employ1);
}
@GetMapping("/list/employ")
private List<Employ> getAllBooks()
{
return employRepository.findAll();
}
}
Application.Properties file
#spring.datasource.url=jdbc:mysql://localhost:3306/waseem #spring.datasource.username=root #spring.datasource.password= #and other credentials spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/blog spring.datasource.username=root spring.datasource.password= Main Class
package com.onetomany.demoonetomany;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class DemoonetomanyApplication {
public static void main(String[] args) {
SpringApplication.run(DemoonetomanyApplication.class, args);
}
}
Get All List of Employ![]()