Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1535

Simple Repository delete of unexisting entity generates insert + delete SQL

    Details

      Description

      With a simple Repository extending JpaRepository<>  a delete of an Entity not existing in the DB will generate 2 SQL request : an insert = a delete.

      This seems dangerous, why create an Entity to delete it afterward?

      It you have constraints your insert could fail, and the error would really be unclear ...

      JPA behavior for remove() : you have to give a managed Entity, so you never have that case.

       

       

      @Entity
      public class Info {
      
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Id
       private Long id;
      
       @Version
       private int version;
      
       private String message;
       // ....
      }
       
      

       

      public interface InfoRepository extends JpaRepository<Info, Long> {}

       

      @RunWith(SpringRunner.class)
      @SpringBootTest(classes = BusinessConfig.class)
      public class InfoRepositoryTest {
      
       private @Autowired InfoRepository infoRepository;
      
       @Test
       public void remove_notExistEntity_Ko() {
       Info info = new Info("a new message");
       infoRepository.delete(info);
       }
      
      }

      Logs  for    infoRepository.delete(info);

      DEBUG org.hibernate.SQL - insert into info (message, version, id) values (?, ?, ?)
      TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [a new message]
      TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [INTEGER] - [0]
      TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [1]
      DEBUG org.hibernate.SQL - delete from info where id=? and version=?
      TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1]
      TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [INTEGER] - [0]

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                schauder Jens Schauder
                Reporter:
                gpeel gpeel
                Last updater:
                Mark Paluch
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: