我在 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/