java - 在 Spring Query 注释中使用枚举作为命名参数

标签 java hibernate spring-boot enums named-parameters

我在 spring JpaRepository 中有一个查询,如下所示:

@Query("SELECT COUNT(c) FROM #{#entityName} c "
        + "WHERE c.feature = 'BBN'  OR c.feature = :feature "
        + "AND c.rating <= :rating "
        + "AND c.article.ean = c.identification.article.ean "
        + "AND c.date BETWEEN :from AND :to "
        + "AND c.identification.type IN (:types)") 
Long countAlgorithmMatchesWithRatingAndTypeBetweenDates(
        @Param("rating") Integer rating, 
        @Param("from") Date from, 
        @Param("to") Date to, 
        @Param("feature") FeatureType feature, 
        @Param("types") IdentificationType... types);

我已经为 hibernate 启用了跟踪调试,我可以看到作为枚举的参数没有被绑定(bind):

Hibernate: select count(classifier0_.uuid) as col_0_0_ from matchx.classifier classifier0_ cross join matchx.identification identifica1_ where classifier0_.identification_uuid=identifica1_.uuid and (classifier0_.feature='BBN' or classifier0_.feature=? and classifier0_.rating<=? and classifier0_.article_ean=identifica1_.article_ean and (classifier0_.date between ? and ?) and (identifica1_.type in (?)))
2018-04-05 07:46:43.373 TRACE 11427 --- [nio-8080-exec-9] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [3]
2018-04-05 07:46:43.374 TRACE 11427 --- [nio-8080-exec-9] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [Sun Jan 01 00:00:00 CET 2017]
2018-04-05 07:46:43.374 TRACE 11427 --- [nio-8080-exec-9] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [TIMESTAMP] - [Tue Jan 01 00:00:00 CET 2019]

负责这些枚举的实体的字段注释如下:

@Enumerated(EnumType.STRING)
private FeatureType feature;

数据库中的一切看起来都很好,但 Hibernates 查询似乎因我看不到的原因而出现问题。

我应该将枚举类型作为字符串传递吗?有什么我想念的吗?

最佳答案

我只使用一个枚举参数创建了一个查询。这样做我可以验证该值确实已发送,但它在输出中不可见。

不要盲目相信绑定(bind)参数消息:)

关于java - 在 Spring Query 注释中使用枚举作为命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653248/

相关文章:

java - Hibernate 按属性获取记录不起作用

rest - spring boot 中 Rest 资源的 NotImplementedException

java - Java 的 NetBeans 输出做了太多事情

Java正则表达式和xml标签

java - 批量插入不起作用只是因为 MySQL 驱动程序吗?其他人呢?

java - 物化 View 上的 "Missing table"

postgresql - Docker Compose (TOMCAT + POSTGRES) - Spring Boot 应用程序 : connection issue between containers

hibernate - 由 : javax. validation.ConstraintViolationException 引起:类验证失败

java - 转换线索 : failed to lazily initialize a collection of role: could not initialize proxy - no Session 时发生错误

java - 从logstash插件发送 "json_event"到JAVA