I know this question has been asked often but I'm unable to find a solution. How can I get a generic type class name in a Spring injected repository?
Here it is my base repository
public interface UserRepository extends JpaRepository<User, Long>, IUserRepository<User>{ User findByUsername(String username); } this is the interface
public interface IUserRepository<T> { public List<T> findAllValidEtSiteAndStructure(); } and finally here it is the implementation
public class UserRepositoryImpl<T> implements IUserRepository<T> { @PersistenceContext private EntityManager em; private Class< T > type; @Override public List<T> findAllValidEtSiteAndStructure() { final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication instanceof UserAuthentication) { final User currentUser = ((UserAuthentication) authentication).getDetails(); return (List<T>) em.createQuery("FROM " + type.getName()+ " WHERE site=:site AND structure=:structure AND valid=:valid") .setParameter("site", currentUser.getInstallation().getSite()) .setParameter("structure", currentUser.getInstallation().getStructure()) .setParameter("valid", true) .getResultList(); } return null; } } how can I get type.name? Thanks in advance