SIn san sun
SIn san sun

Reputation: 633

How return column from my DB using Java Spring JPQL

I have a very basic task. I want to return just one column from my table in my DB. Literally, I want the text from my category_name. This is my JPQL cod:|

@Transactional
    @Modifying
    @Query(value = "SELECT category_name FROM Category  WHERE id=:id", nativeQuery = true)
    String findName(@Param("id") long id);

And I have this error:

Modifying queries can only use void or int/Integer as return type!

Upvotes: 1

Views: 38

Answers (1)

Abhishek Patyal
Abhishek Patyal

Reputation: 514

As the error apparently states, you should use @Modifying annotation when you are actually updating/deleting the row. Since you are fetching data from already stored database, you can simply remove this annotation.
You should also remove @Transactional annotation.
https://dzone.com/articles/how-does-spring-transactional is an interesting article to know about how transactional annotation works and when it should be used.

Upvotes: 2

Related Questions