【发布时间】:2018-09-03 21:41:43
【问题描述】:
我正在使用 spring data jpa,我想从 postgres 表中检索特定列,我通过几个链接检索特定列 Spring JPA selecting specific columns 我发现关于 jpql 构造函数表达式查询来检索特定列,
我在存储库类上使用了 jpql 表达式
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);
模型类是 我也在使用构造函数
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 表达式转换为查询,它显示包名异常,当我使用类名时,它在括号附近显示异常,所以我猜 jpql 表达式没有被翻译 我是否缺少任何依赖项任何帮助将不胜感激?
【问题讨论】:
标签: spring postgresql spring-data-jpa jpql