dbarasuk
dbarasuk

Reputation: 17

Native Query in spring boot JPA

I'm using spring boot 2.6 and spring boot JPA version 2.6.4. I was interested in pull last five records from mysql table by using next query

@Query(value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
 public Collection<Produit> getProduit();

Normally in the @Query block, i need to add the nativeQuery = true after the value property but the nativeQuery attribute is not defined. What am i missing?

Please help.

Upvotes: 0

Views: 540

Answers (1)

K3v1n 0X90
K3v1n 0X90

Reputation: 76

When using a native query you need to define a mapping of the values to for example your own type. You could use interface based projections when you only need to read data see interface based projections. Then your could would look like

@Query(nativeQuery=true, value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
Set<Projection> getProduit();

Another thing to check would be what import is used for the @Query annotation, maybe it is the wrong one.

Upvotes: 0

Related Questions