jpql构造函数表达式查询org.postgresql.util.PSQLException
jpql constructor expression query org.postgresql.util.PSQLException
我正在使用 spring 数据 jpa,我想从 postgres table 中检索特定的列,我点击了几个链接来检索特定的列 Spring JPA selecting specific columns
我发现关于 jpql 构造函数表达式查询以检索特定列,
我在存储库上使用了 jpql 表达式 class
public interface DeviceCrudRepository extends CrudRepository<Device,String>{
public Device findByBarcode(String barcode);
@Query(value = "select new com.hello.world.model.Device (d.deviceName,d.deviceType,d.deviceDescription ,d.deviceRentalStatus) from devicedetails d where d.scancode=:scancode", nativeQuery = true)
public Device findDeviceDetailsByBarcode(@Param("d.scancode") String d.scancode);
模型 class 是
我也在使用构造函数
public Device(String deviceName, String deviceType, String deviceDescription, String deviceRentalStatus) {
super();
this.deviceName = deviceName;
this.deviceType = deviceType;
this.deviceDescription = deviceDescription;
this.deviceRentalStatus = deviceRentalStatus;
}
我一直收到异常
org.postgresql.util.PSQLException:错误:“.”处或附近的语法错误。
位置:15
在 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2190) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169) ~[postgresql-42.1.4.jar:42.1.4]
在 org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117) ~[postgresql-42.1.4.jar:42.1.4]
当我检查日志时
生成的查询是
select new com.hello.world.model.Device (d.deviceName,d.deviceType,d.deviceDescription ,d.deviceRentalStatus) from devicedetails d where d.scancode=?
它无法将 jpql 表达式转换为查询,它显示包名称异常,当我使用 class 名称时它在括号附近显示异常,所以我猜 jpql 表达式没有被翻译
我是否遗漏了任何依赖项,我们将不胜感激?
因为 PGSQL 不是 JPQL 它不理解您的查询是正常的 :) 删除 nativeQuery = true
.
我正在使用 spring 数据 jpa,我想从 postgres table 中检索特定的列,我点击了几个链接来检索特定的列 Spring JPA selecting specific columns 我发现关于 jpql 构造函数表达式查询以检索特定列,
我在存储库上使用了 jpql 表达式 class
public interface DeviceCrudRepository extends CrudRepository<Device,String>{
public Device findByBarcode(String barcode);
@Query(value = "select new com.hello.world.model.Device (d.deviceName,d.deviceType,d.deviceDescription ,d.deviceRentalStatus) from devicedetails d where d.scancode=:scancode", nativeQuery = true)
public Device findDeviceDetailsByBarcode(@Param("d.scancode") String d.scancode);
模型 class 是 我也在使用构造函数
public Device(String deviceName, String deviceType, String deviceDescription, String deviceRentalStatus) {
super();
this.deviceName = deviceName;
this.deviceType = deviceType;
this.deviceDescription = deviceDescription;
this.deviceRentalStatus = deviceRentalStatus;
}
我一直收到异常
org.postgresql.util.PSQLException:错误:“.”处或附近的语法错误。 位置:15 在 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2190) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117) ~[postgresql-42.1.4.jar:42.1.4]
当我检查日志时 生成的查询是
select new com.hello.world.model.Device (d.deviceName,d.deviceType,d.deviceDescription ,d.deviceRentalStatus) from devicedetails d where d.scancode=?
它无法将 jpql 表达式转换为查询,它显示包名称异常,当我使用 class 名称时它在括号附近显示异常,所以我猜 jpql 表达式没有被翻译 我是否遗漏了任何依赖项,我们将不胜感激?
因为 PGSQL 不是 JPQL 它不理解您的查询是正常的 :) 删除 nativeQuery = true
.