[英]Custom save method with Spring Data 4 JPA
我知道春天的數據使用SimpleJpaRepository
作為實施JpaRepository
。 我期待為某些存儲庫覆蓋其save
方法。 有什么方法不意味着替換我所有存儲庫的默認實現?
我已經嘗試在MyEntityJpaImpl
類中擴展SimpleJpaRepository
,但是它沒有提供適合自動裝配的默認構造函數。
編輯:
這是我要自動接線的課程
public class MyEntityJpaImpl<ClientesCentro, ClientesCentroPK extends Serializable> extends SimpleJpaRepository<ClientesCentro, ClientesCentroPK> implements ExtendedRepository<ClientesCentro, ClientesCentroPK>{
private JpaEntityInformation<ClientesCentro, ClientesCentroPK> entityInformation;
@PersistenceContext
private EntityManager em;
private Class<ClientesCentro> clazz;
public MyEntityJpaImpl(Class<ClientesCentro> domainClass, EntityManager em) {
this((JpaEntityInformation<ClientesCentro, ClientesCentroPK>) JpaEntityInformationSupport.getMetadata(domainClass, em), em);
this.clazz = domainClass;
}
public MyEntityJpaImpl(JpaEntityInformation<ClientesCentro, ClientesCentroPK> jpaEntityInformation, EntityManager entityManager) {
super((JpaEntityInformation<ClientesCentro, ClientesCentroPK>) jpaEntityInformation, entityManager);
this.entityInformation = jpaEntityInformation;
this.clazz = this.entityInformation.getJavaType();
this.em = entityManager;
}
public void refresh() {
}
@Transactional
@Override
public <S extends ClientesCentro> S save(S entity) {
System.out.println("Hello world!");
if (entityInformation.isNew((ClientesCentro) entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
}
和最相關的stacktrace部分:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxxyyy.erp.dal.repository.domain.MyEntityJpaImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.xxxyyy.erp.dal.repository.domain.MyEntityJpaImpl.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1094)
... 55 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.xxxyyy.erp.dal.repository.domain.MyEntityJpaImpl.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 56 common frames omitted
您可以通過在擴展JpaRepository的接口內編寫自己的方法來提供自定義實現
有兩種方法可以做到這一點:
1。 您可以使用@Query批注並在其中編寫查詢。
@Query("-----Query here------")
public List<Employee> getEmp();
2。 您可以使用NamedQuery並引用您的方法。
@Query(name="HQL_GET_ALL_EMPLOYEE_BY_ID")
public List<Employee> getEmpByIdUsingNamedQuery(@Param("empId") Long
empId);
spring-data的文檔中包含一些示例,可以完全解決您的問題。
例子30.覆蓋save(...)的片段
interface CustomizedSave<T> {
<S extends T> S save(S entity);
}
class CustomizedSaveImpl<T> implements CustomizedSave<T> {
public <S extends T> S save(S entity) {
// Your custom implementation
}
}
The following example shows a repository that uses the preceding repository fragment:
例子31.定制的倉庫接口
interface UserRepository extends CrudRepository<User, Long>, CustomizedSave<User> {
}
interface PersonRepository extends CrudRepository<Person, Long>, CustomizedSave<Person> {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.