-
Notifications
You must be signed in to change notification settings - Fork 52
-
I am already using another spring data JPA extension in my project, the library is hosted at this address if anyone is interested: https://github.com/darrachequesne/spring-data-jpa-datatables.
Is it possible to use jpa-entity-graph in conjunction with the other library?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 1 reply
-
Hi @sanemain ,
This library repository factory bean currently looks like this:
public class EntityGraphJpaRepositoryFactoryBean< R extends Repository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> { /** * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface. * * @param repositoryInterface must not be {@literal null}. */ public EntityGraphJpaRepositoryFactoryBean(Class<? extends R> repositoryInterface) { super(repositoryInterface); } @Override public void setEntityManager(EntityManager entityManager) { /* Make sure to use the EntityManager able to inject captured EntityGraphs */ super.setEntityManager(RepositoryEntityManagerEntityGraphInjector.proxy(entityManager)); } @Override protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { return new EntityGraphJpaRepositoryFactory(entityManager); } }
IMO, the easiest and most naive solution would be to copy its content to your own RepositoryFactoryBean like this:
public class MyCustomRepositoryFactoryBean< R extends Repository<T, I>, T, I extends Serializable> extends DataTablesRepositoryFactoryBean<R, T, I> { public EntityGraphJpaRepositoryFactoryBean(Class<? extends R> repositoryInterface) { super(repositoryInterface); } @Override public void setEntityManager(EntityManager entityManager) { super.setEntityManager(RepositoryEntityManagerEntityGraphInjector.proxy(entityManager)); } @Override protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { return new EntityGraphJpaRepositoryFactory(entityManager); } }
Notice that the only difference is that this custom implementation extends DataTablesRepositoryFactoryBean
instead of JpaRepositoryFactoryBean
.
Then you would enable it like this:
@Configuration @EnableJpaRepositories(repositoryFactoryBeanClass = MyCustomRepositoryFactoryBean.class) public class MyConfiguration {}
I think a more elegant solution would require to discuss this with the Spring Data team.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks for the quick response @reda-alaoui . I am not sure if I understood your solution, DataTablesRepositoryFactoryBean
has its own implementation of method protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager)
. If I extend DataTablesRepositoryFactoryBean
class and override this method as suggested in your reply, this would, I believe, disable the datatables functionality all together. Am I missing something here?
The solution that comes to my mind is to write a custom repository implementation class that extends DataTables and EntityGraph repository interfaces, and delegate to corresponding implementations in methods. Something like the following, for instance:
public class CustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements EntityGraphJpaRepository<T, ID>, EntityGraphJpaSpecificationExecutor<T>, DataTablesRepository<T, ID> { private EntityGraphJpaRepository<T, ID> entityGraphRepository; private DataTablesRepository<T, ID> dataTablesRepository; public CustomRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); } @Override public Page<T> findAll(Pageable pageable, EntityGraph entityGraph) { return entityGraphRepository.findAll(pageable, entityGraph); } //..... @Override public DataTablesOutput<T> findAll(DataTablesInput input) { return dataTablesRepository.findAll(input); } //..... }
This is not enough for sure, I also need to create a new JpaRepositoryFactoryBean
, copy some code from EntityGraphJpaRepositoryFactoryBean
, etc. I agree that it is best to discuss with Spring Data team for a more elegant solution.
Beta Was this translation helpful? Give feedback.
All reactions
-
@reda-alaoui I got stuck while proceeding with the solution mentioned in my reply. The reason is that RepositoryEntityManagerEntityGraphInjector
and RepositoryMethodEntityGraphExtractor
classes are package private, I need access to them to integrate both JPA extensions.
Beta Was this translation helpful? Give feedback.
All reactions
-
The solution that comes to my mind is to write a custom repository implementation class that extends DataTables and EntityGraph repository interfaces, and delegate to corresponding implementations in methods.
Yes you are correct, I missed this part. The solution is now very britle.
I got stuck while proceeding with the solution mentioned in my reply. The reason is that RepositoryEntityManagerEntityGraphInjector and RepositoryMethodEntityGraphExtractor classes are package private, I need access to them to integrate both JPA extensions.
I'd like to avoid turning those classes into public classes :/
Beta Was this translation helpful? Give feedback.