Here is the sample Generic Repository implementation which extends the spring PagingAndSortingRepository,
@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
public List<T> findByNamedQuery( String name );
public List<T> findByNamedQueryAndParams( String name, Map<String, Object> params );
public T findOneByNamedQuery( String name );
public T findOneByNamedQueryAndParams( String name, Map<String, Object> params );
}
Factory Bean class,
public class GenericRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends
JpaRepositoryFactoryBean<R, T, I> {
@SuppressWarnings( "rawtypes" )
protected RepositoryFactorySupport createRepositoryFactory( EntityManager em )
{
return new MyRepositoryFactory(em);
}
private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private final EntityManager em;
public MyRepositoryFactory( EntityManager em )
{
super(em);
this.em = em;
}
@SuppressWarnings( "unchecked" )
protected Object getTargetRepository( RepositoryMetadata metadata )
{
return new GenericRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), em);
}
protected Class<?> getRepositoryBaseClass( RepositoryMetadata metadata )
{
return GenericRepositoryImpl.class;
}
}
}
Implementation class,
public final class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements
GenericRepository<T, ID> {
private final EntityManager em;
private final Class<T> domainClass;
public GenericRepositoryImpl( Class<T> domainClass, EntityManager entityManager )
{
super(domainClass, entityManager);
this.em = entityManager;
this.domainClass = domainClass;
}
@Override
public List<T> findByNamedQuery( final String name )
{
validate(name);
return this.em.createNamedQuery(name, domainClass).getResultList();
}
@Override
public T findOneByNamedQuery( String name )
{
validate(name);
return this.em.createNamedQuery(name, domainClass).getSingleResult();
}
@Override
public List<T> findByNamedQueryAndParams( String name, Map<String, Object> params )
{
validate(name, params);
final TypedQuery<T> query = this.em.createQuery(name, domainClass);
setParams(query, params);
return query.getResultList();
}
}
So when i try to Autowire GenericRepository into services for different types like Customer.java, Message.java etc it is throwing requires at least one bean type of interface GenericRepository. This works if when I create individual repositories for both customer and message type. Without creating multiple repositories, i am not able to implement this.
@Service
@Transactional( noRollbackFor = Exception.class )
public class CustomerService {
@Autowired
private GenericRepository<Customer, Serializable> cr; works fine with just one entity type
@Autowired
private GenericRepository<Message, Serializable> cr; throws exception
If have 100 or more entity classes, then i end up creating 100's of repositories and which is bad. Please let me know if there is a better way to do this.
See Question&Answers more detail:
os