Customizing Spring Data JPA Repository
Join the DZone community and get the full member experience.
Join For Free<jpa:repositories base-package="example.borislam.dao" factory-class="example.borislam.data.springData.DefaultRepositoryFactoryBean/>
@NoRepositoryBean public interface GenericRepository <T, ID extends Serializable> extends JpaRepository<T, ID> { }
@SuppressWarnings("unchecked")
@NoRepositoryBean
public class GenericRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> , Serializable{
private static final long serialVersionUID = 1L;
static Logger logger = Logger.getLogger(GenericRepositoryImpl.class);
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
private final DefaultPersistenceProvider provider;
private Class<?> springDataRepositoryInterface;
public Class<?> getSpringDataRepositoryInterface() {
return springDataRepositoryInterface;
}
public void setSpringDataRepositoryInterface(
Class<?> springDataRepositoryInterface) {
this.springDataRepositoryInterface = springDataRepositoryInterface;
}
/**
* Creates a new {@link SimpleJpaRepository} to manage objects of the given
* {@link JpaEntityInformation}.
*
* @param entityInformation
* @param entityManager
*/
public GenericRepositoryImpl (JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager , Class<?> springDataRepositoryInterface) {
super(entityInformation, entityManager);
this.entityInformation = entityInformation;
this.em = entityManager;
this.provider = DefaultPersistenceProvider.fromEntityManager(entityManager);
this.springDataRepositoryInterface = springDataRepositoryInterface;
}
/**
* Creates a new {@link SimpleJpaRepository} to manage objects of the given
* domain type.
*
* @param domainClass
* @param em
*/
public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
this(JpaEntityInformationSupport.getMetadata(domainClass, em), em, null);
}
public <S extends T> S save(S entity)
{
if (this.entityInformation.isNew(entity)) {
this.em.persist(entity);
flush();
return entity;
}
entity = this.em.merge(entity);
flush();
return entity;
}
public T saveWithoutFlush(T entity)
{
return
super.save(entity);
}
public List<T> saveWithoutFlush(Iterable<? extends T> entities)
{
List<T> result = new ArrayList<T>();
if (entities == null) {
return result;
}
for (T entity : entities) {
result.add(saveWithoutFlush(entity));
}
return result;
}
}
public class DefaultRepositoryFactoryBean <T extends JpaRepository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> { /** * Returns a {@link RepositoryFactorySupport}. * * @param entityManager * @return */ protected RepositoryFactorySupport createRepositoryFactory( EntityManager entityManager) { return new DefaultRepositoryFactory(entityManager); } } /** * * The purpose of this class is to override the default behaviour of the spring JpaRepositoryFactory class. * It will produce a GenericRepositoryImpl object instead of SimpleJpaRepository. * */ public class DefaultRepositoryFactory extends JpaRepositoryFactory{ private final EntityManager entityManager; private final QueryExtractor extractor; public DefaultRepositoryFactory(EntityManager entityManager) { super(entityManager); Assert.notNull(entityManager); this.entityManager = entityManager; this.extractor = DefaultPersistenceProvider.fromEntityManager(entityManager); } @SuppressWarnings({ "unchecked", "rawtypes" }) protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository( RepositoryMetadata metadata, EntityManager entityManager) { Class<?> repositoryInterface = metadata.getRepositoryInterface(); JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType()); if (isQueryDslExecutor(repositoryInterface)) { return new QueryDslJpaRepository(entityInformation, entityManager); } else { return new GenericRepositoryImpl(entityInformation, entityManager, repositoryInterface); //custom implementation } } @Override protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { if (isQueryDslExecutor(metadata.getRepositoryInterface())) { return QueryDslJpaRepository.class; } else { return GenericRepositoryImpl.class; } } /** * Returns whether the given repository interface requires a QueryDsl * specific implementation to be chosen. * * @param repositoryInterface * @return */ private boolean isQueryDslExecutor(Class<?> repositoryInterface) { return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class .isAssignableFrom(repositoryInterface); } }
public interface MyRepository <T, ID extends Serializable> extends GenericRepository <T, ID> { void someCustomMethod(ID id); }
In next post, I will show you how to add hibernate filter features to this GenericRepository.
Published at DZone with permission of Boris Lam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments