Я знаю, что этот вопрос задавали часто, но я не могу найти решение. Как получить общее имя класса типа в репозитории, вложенном в Spring?Spring get generic type class
Вот это моя база хранилище
public interface UserRepository extends JpaRepository<User, Long>, IUserRepository<User>{
User findByUsername(String username);
}
это интерфейс
public interface IUserRepository<T> {
public List<T> findAllValidEtSiteAndStructure();
}
и, наконец, здесь реализация
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;
}
}
как я могу получить type.name? Заранее спасибо
спасибо, это помогло мне много! – davidetrapani