我知道Spring Data使用SimpleJpaRepository
作为JpaRepository
的实现。我期待覆盖某些存储库的save
方法。有什么方法并不意味着替换我所有存储库的默认实现?
我已经尝试在SimpleJpaRepository
类中扩展MyEntityJpaImpl
,但它没有提供适合自动装配的默认构造函数。
编辑:
以下是我尝试自动装配的课程
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
答案 0 :(得分:0)
您可以通过在扩展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);
答案 1 :(得分:0)
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> {
}