spring data jpa annotation examples (#4309)
This commit is contained in:
parent
3925638b74
commit
8e36e2b155
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.annotations;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
@NoRepositoryBean
|
||||
public interface MyUtilityRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
|
||||
|
||||
Optional<T> findById(ID id);
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package org.baeldung.annotations;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.NamedStoredProcedureQueries;
|
||||
import javax.persistence.NamedStoredProcedureQuery;
|
||||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.StoredProcedureParameter;
|
||||
|
||||
import org.baeldung.persistence.multiple.model.user.User;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
|
||||
@Entity
|
||||
@NamedStoredProcedureQueries({
|
||||
@NamedStoredProcedureQuery(
|
||||
name = "count_by_name",
|
||||
procedureName = "person.count_by_name",
|
||||
parameters = {
|
||||
@StoredProcedureParameter(
|
||||
mode = ParameterMode.IN,
|
||||
name = "name",
|
||||
type = String.class),
|
||||
@StoredProcedureParameter(
|
||||
mode = ParameterMode.OUT,
|
||||
name = "count",
|
||||
type = Long.class)
|
||||
})
|
||||
})
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Transient
|
||||
private int age;
|
||||
|
||||
@CreatedBy
|
||||
private User creator;
|
||||
|
||||
@LastModifiedBy
|
||||
private User modifier;
|
||||
|
||||
@CreatedDate
|
||||
private Date createdAt;
|
||||
|
||||
@LastModifiedBy
|
||||
private Date modifiedAt;
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.baeldung.annotations;
|
||||
|
||||
import javax.persistence.LockModeType;
|
||||
|
||||
import org.springframework.data.jpa.repository.Lock;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.query.Procedure;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PersonRepository extends MyUtilityRepository<Person, Long> {
|
||||
|
||||
@Lock(LockModeType.NONE)
|
||||
@Query("SELECT COUNT(*) FROM Person p")
|
||||
long getPersonCount();
|
||||
|
||||
@Query("FROM Person p WHERE p.name = :name")
|
||||
Person findByName(@Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT AVG(p.age) FROM person p", nativeQuery = true)
|
||||
Person getAverageAge();
|
||||
|
||||
@Procedure(name = "count_by_name")
|
||||
long getCountByName(@Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE Person p SET p.name = :name WHERE p.id = :id")
|
||||
void changeName(@Param("id") long id, @Param("name") String name);
|
||||
|
||||
}
|
Loading…
Reference in New Issue