1

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 Answer 1

2

Because PGSQL is not JPQL it's normal that it do not understand your query :) Remove the nativeQuery = true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.