Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法在JPA存储库的@Query注释中使用常量(EnumType Ordinal)值?_Java_Hibernate_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Java 有没有办法在JPA存储库的@Query注释中使用常量(EnumType Ordinal)值?

Java 有没有办法在JPA存储库的@Query注释中使用常量(EnumType Ordinal)值?,java,hibernate,spring-boot,jpa,spring-data-jpa,Java,Hibernate,Spring Boot,Jpa,Spring Data Jpa,枚举 实体类 public enum CountEnum { ONE, TWO } 我想查询所有包含countEnum的Test行'ONE'。但由于这里的@Enumerated(EnumType.ORDINAL)是有序的,我必须在@Query中输入'ONE'的int值,而不是字符串 我的存储库界面 @Entity public class Test { ... @Enumerated(EnumType.ORDINAL) private CountEnum count

枚举

实体类

public enum CountEnum {
   ONE,
   TWO
}
我想查询所有包含countEnum的
Test
'ONE'
。但由于这里的
@Enumerated(EnumType.ORDINAL)
是有序的,我必须在
@Query
中输入
'ONE'
的int值,而不是字符串

我的存储库界面

@Entity
public class Test {
...
    @Enumerated(EnumType.ORDINAL)
    private CountEnum countEnum;
...
}
公共接口资源库扩展了JpaRepository{
@查询(“从test test中选择test,其中test.countEnum=“+countEnum.ONE.ordinal())
列表查找();
}

但它会抛出一个错误,即属性值必须是常量。既然我不想输入硬编码的常量值,那个么如何使用enum的序数值查询所有这些行呢

使用常规方法,如:

public interface ResourceRepository extends JpaRepository<Test, String> {
    @Query(" select test from Test test where test.countEnum = " + CountEnum.ONE.ordinal())
    List<Test> find();
}
公共接口资源库扩展了JpaRepository{
列出findByCountEnum(CountEnum ce);
}

将查询定义为字符串常量:

public interface ResourceRepository extends JpaRepository<Test, String> {

    List<Test> findByCountEnum(CountEnum ce);
}
然后在注释中使用常量:

private static final String QUERY_FIND = " select test from Test test where test.countEnum = " + CountEnum.ONE.ordinal();
公共接口资源库扩展了JpaRepository{
@查询(查询查找)
列表查找();
}

如果将常量值指定给枚举:

public interface ResourceRepository extends JpaRepository<Test, String> {
    @Query(QUERY_FIND)
    List<Test> find();
}
然后可以在
@Query
注释中使用enum:

public enum CountEnum {
    ONE(Constants.ONE_VALUE), TWO(Constants.TWO_VALUE);

    CountEnum(int countEnum) {
    }

    public static class Constants  {
        public static final int ONE_VALUE = 0;
        public static final int TWO_VALUE = 1;
    }
}
公共接口资源库扩展了JpaRepository{
@查询(“从test test中选择test,其中test.countEnum=“+countEnum.Constants.ONE_值”)
列表查找();
}

为什么您认为在编写JPQL时必须使用序数值

JPA规范规定:

4.6.1文字 [……] 枚举文字支持使用Java枚举文字语法。必须指定完全限定的枚举类名

因此,我希望类似以下的方法能够奏效:

public interface ResourceRepository extends JpaRepository<Test, String> {
    @Query(" select test from Test test where test.countEnum = " + CountEnum.Constants.ONE_VALUE)
    List<Test> find();
}
公共接口资源库扩展了JpaRepository{
@查询(“从test test中选择test,其中test.countEnum=com.somepackage.with.sub.pakcages.countEnum.ONE”)
列表查找();
}

对我不起作用,尽管它似乎完全有效,并且是我最初尝试的方式。获取
org.hibernate.hql.internal.ast.QuerySyntaxException:无效路径:“com.example.Class.Enum”
。可能是内部枚举造成的?
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();
}