Saturday, 17 August 2019

Spring Data: Repository pattern

Spring Data uses repository pattern, with two bounded type parameters T, ID.

public interface Repository<T, ID> {

}

T: Domain type that the repository manages. For ex, Employee, Address, Organization etc.,

ID: Type of the id of the entity the repository manages

For example for the below 'Employee' entity, T specifies Employee and ID specifies Integer.
@Entity
public class Employee {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private int id;

 ....
 ....
}

We can define EmployeeRepository like below.
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {

 
}


'CrudRepository' interface is provided by spring data which extends Repository interface and provide methods to create, read, update and delete records from database.


Previous                                                    Next                                                    Home

No comments:

Post a Comment