I use Spring data jpa and I am trying to add custom behaviour to all repositories as described here:
http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories
I encountered several issues:
-First, there is no such method as getDomainClass in the RepositoryMetadata class as described in the Spring documentation (see below):
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), entityManager);
}
I used the following method instead: getDomainType() Is this right?
-Second my application throws exceptions when tomcat starts. Here is the full stack trace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalRepositoryImpl' defined in file [E:\users\jumartin\dev_sts\.metadata\.plugins\org.eclipse.wst.server.core\
tmp0\wtpwebapps\SuiviTRC\WEB-INF\classes\trc\suivi\repository\GlobalRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could
not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<i
nit>()
Here is my custom global repository code:
public class GlobalRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GlobalRepository<T, ID> {
private EntityManager em;
public GlobalRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
this.em = em;
}
public void sharedCustomMethod(ID id) {
}
}
Here is my xml config:
<repositories base-package="trc.suivi.repository" factory-class="trc.suivi.repository.GlobalRepositoryFactoryBean">
<repository id="pliRepository" />
<repository id="globalRepository" />
</repositories>
I was not able to find any other sample on the web. Can anyone please help?
I finally got some help and was able to get my repository to work by using the @NoRepositoryBean annotation on the intermediate interface.
Further info is available here: http://forum.springsource.org/showthread.php?128536-Several-issues-with-quot-adding-custom-behaviour-to-all-repositories-quot-in-spring-data-jpa