Spring Data (JPA) - Using Generics in @Query

I wonder if it is possible to use generics in named queries in spring data (using jpa alone), is it possible to do something like this?

@NoRepositoryBean public interface EnumerationRepository<T extends Enumeration> extends JpaRepository<T,Integer> { @Query("Select t.type from T t") public List<String> selectTypes(); } 

The enumeration class being this

 @MappedSuperclass public abstract class Enumeration { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", length = 3) private int id; @Column(name = "type", nullable = false, unique = true, length = 30) private String type; // getters / setters .. etc } 

I just skipped some fields in the Enumeration class.

Tried this, but obviously he complains that class T is not mapped.

The fact is that I have 20+ tables that share some basic structure, and since I need queries to retrieve only the data from the columns, and not the whole object, it would be nice to find a way to get the query into the "parent" repository and should not repeat the code 20 times.

+7
generics spring-data-jpa
source share
1 answer

When using Spring data from JPA 1.4 or higher, the following will work:

 @Query("Select t.type from #{#entityName} t") 
+14
source share

All Articles