In Spring Boot, a one-to-many relationship is a common association where one entity has a relationship with multiple instances of another entity. Let’s go through an example of a one-to-many relationship using Spring Boot and JPA (Java Persistence API).
Consider two entities: Author
and Book
. An author can have multiple books, creating a one-to-many relationship.
- Create the Author Entity:
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Entity
@Data
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
private List<Book> books;
}
- Create the Book Entity:
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
}
In the Author
class, we use the @OneToMany
annotation to define the one-to-many relationship with the Book
class. The mappedBy
attribute indicates the field in the Book
class that owns the relationship (i.e., the author
field). The cascade
attribute is set to CascadeType.ALL
, which means that any changes made to the Author
entity will be propagated to its associated Book
entities.
In the Book
class, we use the @ManyToOne
annotation to define the many-to-one side of the relationship. The @JoinColumn
annotation is used to specify the foreign key column in the Book
table that refers to the primary key column in the Author
table.
- Create Repositories:
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorRepository extends JpaRepository<Author, Long> {
}
public interface BookRepository extends JpaRepository<Book, Long> {
}
- Create Service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LibraryService {
@Autowired
private AuthorRepository authorRepository;
@Autowired
private BookRepository bookRepository;
public Author saveAuthor(Author author) {
return authorRepository.save(author);
}
public Book saveBook(Book book) {
return bookRepository.save(book);
}
public List<Author> getAllAuthors() {
return authorRepository.findAll();
}
}
- Usage in Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/library")
public class LibraryController {
@Autowired
private LibraryService libraryService;
@PostMapping("/author")
public Author createAuthor(@RequestBody Author author) {
return libraryService.saveAuthor(author);
}
@PostMapping("/book")
public Book createBook(@RequestBody Book book) {
return libraryService.saveBook(book);
}
@GetMapping("/authors")
public List<Author> getAllAuthors() {
return libraryService.getAllAuthors();
}
}
This example provides a simple implementation of a one-to-many relationship in Spring Boot using JPA. You can extend this example by adding more features, error handling, and additional methods to manage the relationships between authors and books in a more comprehensive way.