Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to use this library together with another JPA Data extension? #50

Unanswered
sanemain asked this question in Q&A
Discussion options

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?

You must be logged in to vote

Replies: 3 comments 1 reply

Comment options

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.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
1 reply
Comment options

@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.

Comment options

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 :/

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /