Search code examples
spring-bootspring-data-jpajpql

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!

Solution

  • 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.