I'll duplicate my answer here because it's the same as another similar question:
It's my opinion based on my recent experience to make reflection in some classes or interfaces in Spring Boot.
As @ekcrisp said in his answer in similar question:
You can use Spring's GenericTypeResolver to get the Entity class from your Repository.
My experience: unfortunately the common ways of reflection don't work to get the <T>
entity class in a Spring Boot's repository. I tried in a lot of maneers. But this GenericTypeResolver
actually could get the generics <T>
type from JpaRepository<T, ID>
.
In other words, the function below can provide all generic types of a class as an array. Then you can choose one of them:
public Class<?>[] getGenericType( Class<?> classInstance, Class<?> classToGetGenerics ) {
return GenericTypeResolver.resolveTypeArguments( classInstance, classToGetGenerics );
}
This answer is about this stack and its versions:
JDK 11, Spring Boot 2.7.11 with Spring MVC, Spring Data JPA, and others libs. Posted in 2023-05-11.
If you want to add a solution in your "ObjectUtil" local solution, maybe these methods can be useful to get generic types, in any position, like get <T>
or <ID>
in JpaRepository<T, ID>
:
@UtilityClass
public class ObjectUtil {
// ...
/**
*
* @param classInstance
* @param classToGetGenerics
*
* @return the generic classes of the given param, based on class to get generics param.
*
* @see GenericTypeResolver#resolveTypeArguments
*/
public static Class<?>[] getGenericType( Class<?> classInstance, Class<?> classToGetGenerics ) {
return GenericTypeResolver.resolveTypeArguments( classInstance, classToGetGenerics );
}
/**
*
* @param classInstance
* @param classToGetGenerics
* @param genericPosition
*
* @return the generic class of the given param, based on class to get generics and generic position params.
*
* @see ObjectUtil#getGenericType
* @see GenericTypeResolver#resolveTypeArguments
*/
public static Class<?> getGenericType( Class<?> classInstance, Class<?> classToGetGenerics, int genericPosition ) {
Class<?>[] typeArguments = getGenericType( classInstance, classToGetGenerics );
if( typeArguments != null && typeArguments.length >= genericPosition ) {
return typeArguments[genericPosition];
}
throw new IllegalArgumentException( "Could not determine generic type for interface " + classInstance.getName() );
}
// ...
}
So we can use like the example below (I created a QueryUtil to handle specifically persistence stuff):
@UtilityClass
public class QueryUtil {
// ...
/**
*
* @param repositoryClass
*
* @return the @Entity class of the given repository param.
*
* @see ObjectUtil#getGenericType(Class, Class, int)
* @see ObjectUtil#getGenericType(Class, Class)
*/
public static Class<?> getRepositoryEntityType( Class<?> repositoryClass ) {
return ObjectUtil.getGenericType( repositoryClass, Repository.class, 0 );
}
}
I hope this is still useful. Here it was used in a customized audit logic, made by aspects, so that the Entity is audited.
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select 'SampleEnumeration']