Write HQL UPDATE query with generic type

jer*_*eca 2 java spring hibernate hql spring-boot

I have a generic tokenRepository interface :

public interface TokenRepository<T_Token extends Token, T_id> {

   @Modifying
   @Query("UPDATE T_token as a SET a.revocationReason = :reason WHERE a.id = :id")
   void revokeByTokenId (@Param("id") T_id id, @Param("reason") RevocationReason revocationReason);

}
Run Code Online (Sandbox Code Playgroud)

And a specialized repository interface :

public interface CustomerTokenRepository extends Repository<CustomerToken, Long>, TokenRepository<CustomerToken, Long> {}
Run Code Online (Sandbox Code Playgroud)

When I launch the Spring Boot app, hibernate return the following error :

org.hibernate.hql.internal.ast.QuerySyntaxException: T_token is not mapped [UPDATE T_token as a SET a.revocationReason = :reason WHERE a.id = :id]
Run Code Online (Sandbox Code Playgroud)

So my question is : Is it possible and how to use java generic type with HQL ?

Thanks !

小智 5

Yes it's possible

@Modifying
   @Query("UPDATE #{#entityName} as a SET a.userRevocationReason = :reason WHERE a.refreshToken.id = :rid")
   void revokeByRefreshToken (@Param("rid") T_id refreshTokenId, @Param("reason") UserRevocationReason userRevocationReason);
Run Code Online (Sandbox Code Playgroud)

Where entity name is @Entity(name = "my_entit_name")