
- Spring Boot ORM - Home
- Spring Boot ORM - Overview
- Environment Setup
- Spring Boot ORM - JPA
- Spring Boot ORM & Spring Data JPA
- Spring Boot ORM - Create Project
- Application.properties
- Spring Boot ORM - Update Project
- Spring Boot ORM - Test Hibernate
- Spring Boot ORM & EclipseLink
- Maven EclipseLink
- Update Project EclipseLink
- Spring Boot ORM - Test EclipseLink
Spring Boot ORM - JPA
Java Persistence API (JPA) is a collection of classes and interfaces that allow object/relational mapping of Java objects with databases. In JPA, Entity classes are defined in persistence.xml file. Spring Boot does not use persistence.xml. Instead it uses "Entity Scanning" is the process of finding and registering Entity classes with the persistence provider.
What is Entity Scanning?
Entity Discovery − When the provider finds a class with the @Entity annotation, it recognizes it as a persistent entity and registers it.
Persistence Unit − The discovered entities are then associated with a persistence unit, which defines a set of entities managed by a single EntityManager.
Scanning Packages − The JPA provider scans specified packages or directories for classes annotated with the @Entity annotation.
By default the auto-configured packages are scanned.
Configuring Entity Scanning in Spring Boot
-
Default Scanning − By default, Spring Boot scans the package of your main application class and its sub-packages for entity classes.
-
Customizing Scanning − You can use the @EntityScan annotation to specify the packages to scan. For example:
@SpringBootApplication @EntityScan(basePackages = {"com.example.entities"}) public class Application { // ... }
Spring Data JPA Repositories
Spring Data JPA implements interfaces that are used to access data from database (relational, NoSQL or anything else). Spring Data repositories extend from either Repository or CrudRepository.
@Repository public interface EmployeeRepository extends CrudRepository<Employee, Integer> { }
Creating and Deleting Databases
JPA databases are automatically created if you use an embedded database like H2, Derby. To create and drop tables, your application.properties should look like this −
spring.jpa.hibernate.ddl-auto=create-drop
Mapping associations between classes
Mapping associations can be of two types −
Unidirectional association − In this association, only one entity holds a reference to the other. For example, One to Many or Many to One relationship.
Bidirectional association − In a bidirectional association, both entities (source and target) have a relationship field that refers to each other. For example, Many to Many relationship.
One-to-Many Relationship (Unidirectional)
Imagine a Department entity and its Employees. Each department has many employees, but each employee belongs to one department only.
Department.java
package com.tutorialspoint; import javax.persistence.*; import java.util.*; @Entity public class Department { @Id private Long id; @OneToMany @JoinColumn(name = "department_id") private List<Employee> employees; public Department() {} public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
Employee.java
package com.tutorialspoint; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Employee { @Id private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
Many-to-One Relationship (Unidirectional)
Consider a Student and a School. Each student can be enrolled in only one school, but each school can have multiple students.
Student.java
package com.tutorialspoint; import javax.persistence.*; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne @JoinColumn(name = "school_id") private School school; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } }
School.java
package com.tutorialspoint; import javax.persistence.Entity; import javax.persistence.Id; import java.util.List; @Entity public class School { @Id private Long id; private List<Student> students; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
Many-to-Many Relationship (Bidirectional)
In a bidirectional association, both entities (source and target) have a relationship field that refers to each other. Imagine a Book and an Author. Each book has one author, but each author can write multiple books.
Book.java
package com.tutorialspoint; import javax.persistence.*; @Entity public class Book { @Id private Long id; private String title; @ManyToOne @JoinColumn(name = "author_id") private Author author; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } }
Author.java
package com.tutorialspoint; import javax.persistence.*; import java.util.List; @Entity public class Author { @Id private Long id; private String name; @OneToMany(mappedBy = "author") private List<Book> books; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } }