In JPQL, is there a way to delete a row from a database and return a property of the deleted row?
For example, given a database that stores books, I want to delete a book of a given ID and return the book-title.
I was trying to implement a custom query for this in my BookRepository, which extends the JpaRepository:
public interface BookRepository extends JpaRepository<Book, Long> {
@Transactional
@Modifying
@Query("DELETE FROM Book b WHERE b.id = ?1")
String deleteBookById(Long id);
The Query is lacking the part, which returns the book-title. Can someone help me out, please?
CodePudding user response:
No, but HQL will get this at some point in the future. If you are using PostgreSQL, you can use the returning
clause for this purpose:
@Transactional
@Query(value = "DELETE FROM Book b WHERE b.id = ?1 RETURNING title", native = true)
String deleteBookById(Long id);