Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an enum as a named parameter in Spring Query annotation

I have a query in a spring JpaRepository that looks like this:

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

I have enabled trace debugging for hibernate, and I can see that the parameters that are enums are not bound:

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]

The entities responsible for these enums have their fields annotated like this:

@Enumerated(EnumType.STRING)
private FeatureType feature;

Everything looks good in the database, but Hibernates queries seem to be acting up for reasons I can't see.

Should I pass the enum types as strings instead? Is there something I'm missing?

like image 465
jokarl Avatar asked Feb 23 '26 22:02

jokarl


1 Answers

I created a query using only a single parameter that was an enum. Doing that I could verify that the value was indeed sent, but it is not visible in the output.

Do not trust the binding parameter message blindly :)

like image 121
jokarl Avatar answered Feb 26 '26 19:02

jokarl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!