【问题标题】:Write HQL UPDATE query with generic type使用泛型类型编写 HQL UPDATE 查询
【发布时间】:2019-02-01 08:18:35
【问题描述】:

我有一个通用的 tokenRepository 接口:

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);

}

还有一个专门的存储库接口:

public interface CustomerTokenRepository extends Repository<CustomerToken, Long>, TokenRepository<CustomerToken, Long> {}

当我启动 Spring Boot 应用程序时,休眠返回以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: T_token is not mapped [UPDATE T_token as a SET a.revocationReason = :reason WHERE a.id = :id]

所以我的问题是:是否有可能以及如何将 java 泛型类型与 HQL 一起使用?

谢谢!

【问题讨论】:

    标签: java spring hibernate spring-boot hql


    【解决方案1】:

    是的,有可能

    @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);
    

    实体名称为@Entity(name = "my_entit_name")

    【讨论】:

    • 谢谢! @Entity(name = "my_entit_name") 是否必须在 CustomerToken 实体上?在 HQL 文档#{#entityName} 哪里可以找到这个?
    • 是的,我在这里读过这个docs.spring.io/spring-data/jpa/docs/current/reference/html/… 该部分是 5.3.7。使用 SpEL 表达式
    • 确保基础接口仓库使用@NoRepositoryBean注解
    猜你喜欢
    • 2016-09-14
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2018-04-06
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多