-
Introduction
Spring Data provides an abstraction over data storage technologies.
Common Spring Data Annotations
@Transactional
@NoRepositoryBean
@Param
1 | @Query("FROM Person p WHERE p.name = :name") |
@Id
@Id marks a field in a model class as the primary key
1 | class Person { |
@Transient
We can use this annotation to mark a field in a model class as transient. Hence the data store engine won’t read or write this field’s value
1 | class Person { |
@CreatedBy, @LastModifiedBy, @CreatedDate, @LastModifiedDate
With these annotations, we can audit our model classes: Spring automatically populates the annotated fields with the principal who created the object, last modified it, and the date of creation, and last modification:
Note, that if we want Spring to populate the principals, we need to use Spring Security as well.
1 | public class Person { |
Spring Data JPA Annotations
@Query
@Procedure
@Lock
We can configure the lock mode when we execute a repository query method
- READ
- WRITE
- OPTIMISTIC
- OPTIMISTIC_FORCE_INCREMENT
- PESSIMISTIC_READ
- PESSIMISTIC_WRITE
- PESSIMISTIC_FORCE_INCREMENT
- NONE
1 | @Lock(LockModeType.NONE) |
@Modifying
1 | @Modifying |
@EnableJpaRepositories
1 | @Configuration |