Enum,
public enum CountEnum {
ONE,
TWO
}
Entity class,
@Entity
public class Test {
...
@Enumerated(EnumType.ORDINAL)
private CountEnum countEnum;
...
}
I want to query all the Test
rows having countEnum 'ONE'
. But as here @Enumerated(EnumType.ORDINAL)
is ordinal, I have to put int value of 'ONE'
in @Query
instead of String.
My Repository Interface,
public interface ResourceRepository extends JpaRepository<Test, String> {
@Query(" select test from Test test where test.countEnum = " + CountEnum.ONE.ordinal())
List<Test> find();
}
But it throws an error saying Attribute value must be constant
. So how do I will query all those rows using ordinal value of enum as I don't want to put hardcoded constant values?
Why do you think you have to use the ordinal value when writing JPQL?
The JPA Specification says:
4.6.1 Literals
[...] Enum literals support the use of Java enum literal syntax. The fully qualified enum class name must be specified.
I therefore would expect something like the following to work:
public interface ResourceRepository extends JpaRepository<Test, String> {
@Query(" select test from Test test where test.countEnum = com.somepackage.with.sub.pakcages.CountEnum.ONE")
List<Test> find();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With