Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Spring boot 如何使用Spring数据JPA在@Query注释中动态传递列名_Spring Boot_Spring Data Jpa - Fatal编程技术网

Spring boot 如何使用Spring数据JPA在@Query注释中动态传递列名

Spring boot 如何使用Spring数据JPA在@Query注释中动态传递列名,spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我有这样的想法: @Id @Column_name = "abc" int pk; @Column_name = "def" int id; 我的存储库是: interface fetchDataRepository extends jpaRepository<className, int> { @Query("Select S_Test.nextVal from dual"); Long generateId(); } 并在@Query注释内使用如下内容: @Q

我有这样的想法:

@Id
@Column_name = "abc"
int pk;
@Column_name = "def"
int id;
我的存储库是:

interface fetchDataRepository extends jpaRepository<className, int> {
    @Query("Select S_Test.nextVal from dual");
    Long generateId();
}
并在@Query注释内使用如下内容:

@Query("Select :sequenceName.nextVal from dual");
有什么办法吗?如有任何建议,将不胜感激


编辑:无法使用entityName。如果是,那么请告诉我如何替换?

不幸的是,您只能替换在JDBC中可以执行的操作,因此,几乎只能替换INSERT和WHERE子句中的值。不支持动态表、列和架构名称

有一个例外可能适用,那就是。有一个变量可用-entityName。因此,假设Entity类上的@Entity注释的命名与序列相同,您可以使用如下@Query:

@QuerySelect{entityName}.nextVal来自dual; 否则,由于您的查询很简单,并且不涉及任何对象关系映射,因此您可能需要在其中插入一个,以便运行查询


否则,您可以插入EntityManager并尝试使用-但您实际上并没有尝试将resultset映射到实体,因此JdbcTemplate将更简单。

不幸的是,您只能替换在JDBC中可以执行的操作,因此,几乎只替换INSERT和WHERE子句中的值。不支持动态表、列和架构名称

有一个例外可能适用,那就是。有一个变量可用-entityName。因此,假设Entity类上的@Entity注释的命名与序列相同,您可以使用如下@Query:

@QuerySelect{entityName}.nextVal来自dual; 否则,由于您的查询很简单,并且不涉及任何对象关系映射,因此您可能需要在其中插入一个,以便运行查询

否则,您可以插入EntityManager并尝试使用-但您实际上并没有尝试将resultset映射到实体,因此JdbcTemplate将更简单

@Query("Select :sequenceName.nextVal from dual");