jpql constructor expression query org.postgresql.util.PSQLException

时间:2018-03-25 18:57:48

标签: spring postgresql spring-data-jpa jpql

i am using spring data jpa , i want to retrieve specific columns from postgres table, i followed couple of links to retrieve specific columns Spring JPA selecting specific columns i found regarding jpql constructor expression query to retrieve specific columns,

i used the jpql expression on the respository 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);

Model class is i am using constructor too

public Device(String deviceName, String deviceType, String deviceDescription, String deviceRentalStatus) {
        super();
        this.deviceName = deviceName;
        this.deviceType = deviceType;
        this.deviceDescription = deviceDescription;
        this.deviceRentalStatus = deviceRentalStatus;
    }

I keep on getting exception

org.postgresql.util.PSQLException: ERROR: syntax error at or near "." Position: 15 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2190) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169) ~[postgresql-42.1.4.jar:42.1.4] at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117) ~[postgresql-42.1.4.jar:42.1.4]

when i checked in logs the query which was generated was

select new com.hello.world.model.Device (d.deviceName,d.deviceType,d.deviceDescription ,d.deviceRentalStatus) from devicedetails d where d.scancode=?

it's not able to translate the jpql expression into query,it's showing exception for package name, when i use the class name it's showing exception near the brackets, so i guess the jpql expression is not being translated am i missing any dependency any help would be appreciated?

1 个答案:

答案 0 :(得分:1)

因为 PGSQL 不是 JPQL ,它不理解您的查询是正常的:)删除nativeQuery = true