我在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启用了跟踪调试,我可以看到枚举的参数没有绑定:
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查询似乎是出于我无法看到的原因而起作用。
我应该将枚举类型作为字符串传递吗?有什么我想念的吗?
答案 0 :(得分:0)
嗯,你必须考虑这个:
枚举基本上就是宏。 它们是您为其命名的数字,因此您可以在代码中轻松调用它们并使其可读。
如果要保存枚举,请使用myEnum.getValue()
保存其值,该值返回与其调用的枚举相当的int,然后将其保存为数据库中的int。
编辑:好吧,语法......
答案 1 :(得分:0)
您的查询应如下所示,您在where
中只有重复的条件:
@Query("SELECT COUNT(c) FROM #{#entityName} c "
+ "WHERE 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) ")
我删除了此c.feature = 'BBN'
答案 2 :(得分:0)
我只使用一个枚举的参数创建了一个查询。这样做我可以验证值确实已发送,但在输出中不可见。
不要盲目信任binding parameter
消息:)