Reputation: 24679
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?
Upvotes: 3
Views: 2374
Reputation: 64011
As of Spring Data JPA 1.9.M1 it's become easier to add custom methods to all repositories managed by Spring Data. This example has all the details.
In your case the example would look like:
1) Configuration
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(repositoryBaseClass = GlobalRepositoryImpl.class)
class CustomRepositoryConfig {}
2) Custom base repository:
public class GlobalRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GlobalRepository<ID> {
public GlobalRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
}
@Override
public void sharedCustomMethod(ID id) {
}
}
3) Some repository:
public interface SomeRepository extends GlobalRepository<User, Long> {}
Of course GlobalRepository
still needs to be annotated with @NoRepositoryBean
Upvotes: 1
Reputation: 2521
I have made a full example of how to add custom behaviour to all repositories. http://borislam.blogspot.hk/2012/07/customizing-spring-data-jpa-repository.html
You could add features of different JPA implementation (e.g. hibernate, openJPA) to your base repository. I have made another tutorial on that. http://borislam.blogspot.hk/2012/07/adding-hibernate-native-sql-features.html
Upvotes: 2
Reputation: 24679
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
Upvotes: 1