Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.
Allows developers to work with JPA entities without having to write boilerplate code.
Spring Data JPA supports the creation of JPA queries from method names.
Spring Data JPA allows developers to define custom queries using JPQL, SQL, or native queries.
Spring Data JPA supports pagination and sorting for JPA repositories.
Spring Data JPA provides auditing support for JPA entities.
Spring Data JPA provides transaction management support for JPA repositories.
Spring Data JPA supports entity inheritance strategies. There are three inheritance strategies supported by JPA:
- Single Table
- Joined
- Table Per Class
Single Table
In this strategy, all classes in the hierarchy are mapped to a single table in the database.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "employee_type", discriminatorType = DiscriminatorType.STRING)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
private String employeeType;
// Getters and Setters
}
@Entity
@DiscriminatorValue("full_time")
public class FullTimeEmployee extends Employee {
private double salary;
private double bonus;
// Getters and Setters
}
@Entity
@DiscriminatorValue("part_time")
public class PartTimeEmployee extends Employee {
private double hourlyRate;
private double hoursWorked;
// Getters and Setters
}Joined
In this strategy, each class in the hierarchy is mapped to its table in the database.
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
// Getters and Setters
}
@Entity
public class FullTimeEmployee extends Employee {
private double salary;
private double bonus;
// Getters and Setters
}
@Entity
public class PartTimeEmployee extends Employee {
private double hourlyRate;
private double hoursWorked;
// Getters and Setters
}Table Per Class
In this strategy, each class in the hierarchy is mapped to its table in the database.
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
// Getters and Setters
}
@Entity
public class FullTimeEmployee extends Employee {
private double salary;
private double bonus;
// Getters and Setters
}
@Entity
public class PartTimeEmployee extends Employee {
private double hourlyRate;
private double hoursWorked;
// Getters and Setters
}Spring Data JPA supports the following types of entity associations:
- One-to-One
- One-to-Many
- Many-to-One
- Many-to-Many
One-to-One
In this type of association,
- Each record in the source entity is associated with one record in the target entity.
- Implemented using
@OneToOneannotation - Typically, uses a foreign key in the owner entity's table. Owner Entity is the one where the foreign key is created
@Entity
public class AccessCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private LocalDate allocatedDate;
private String accessCode;
// Getters and Setters
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
@OneToOne
private AccessCard accessCard;
// Getters and Setters
}One To Many
In this type of association,
- One record in the source entity is associated with multiple records in the target entity.
- Implemented using
@OneToManyannotation. - Typically, uses a foreign key in the target entity's table. Target Entity is the one where the foreign key is created.
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
// Getters and Setters
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
@ManyToOne
private Department department;
// Getters and Setters
}Many to Many
In this type of association,
- Multiple records in the source entity are associated with multiple records in the target entity.
- Implemented using
@ManyToManyannotation. - Typically, uses a join table to store the association.
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String phone;
@ManyToMany
private List<Course> courses;
// Getters and Setters
}
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@ManyToMany(mappedBy = "courses")
private List<Student> students;
// Getters and Setters
}