0
@Repository
public interface RoomRepository extends CrudRepository<RoomEntity, Long> {
    @Override
    List<RoomEntity> findAll();

    @Modifying
    @Query("INSERT INTO room VALUES(:id, :name)")
    RoomEntity insert(@Param("id") String uuid, @Param("name") String name);
}

I made an insert custom query to use the default key for the UUID. Is there any way for custom queries to receive result values as entities? The above code doesn't work. The framework is returning an int.

  • It is returning the number of rows affected - please have a look here https://stackoverflow.com/questions/23122846/query-returning-object-instead-of-entity – somshivam Apr 30 '20 at 08:25

1 Answers1

0

This is not directly possible.

Some database allow execution of multiple statements: Multiple queries executed in java in single statement

But the infrastructure of Spring Data JDBC is not prepared to handle such a case.

What you may create a custom method to do this by first inserting the row and then selecting it in two steps using a JdbcTemplate or similar mechanism.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348