Spring Data: “delete by” is supported?

后端 未结 9 2234
长情又很酷
长情又很酷 2020-11-28 20:57

I am using Spring JPA for database access. I am able to find examples such as findByName and countByName, for which I dont have to write any method implementation. I am hopi

相关标签:
9条回答
  • 2020-11-28 21:20

    If you will use pre defined delete methods as directly provided by spring JPA then below two queries will be execute by the framework.

    • First collect data(like id and other column) using by execute select query with delete query where clause.

    • then after getting resultSet of first query, second delete queries will be execute for all id(one by one)

      Note : This is not optimized way for your application because many queries will be execute for single MYSQL delete query.

    This is another optimized way for delete query code because only one delete query will execute by using below customized methods.

    
    
    @NamedNativeQueries({
    
    @NamedNativeQuery(name = "Abc.deleteByCreatedTimeBetween",
                query = "DELETE FROM abc WHERE create_time BETWEEN ?1 AND ?2")
        ,
    
        @NamedNativeQuery(name = "Abc.getByMaxId",
                query = "SELECT max(id) from abc")
    })
    
    @Entity
    public class Abc implements Serializable {
    
    }
    
    @Repository
    public interface AbcRepository extends CrudRepository {
    
        int getByMaxId();
    
        @Transactional
        @Modifying
        void deleteByCreatedTimeBetween(String startDate, String endDate);
    }
    
    
    0 讨论(0)
  • 2020-11-28 21:33

    Deprecated answer (Spring Data JPA <=1.6.x):

    @Modifying annotation to the rescue. You will need to provide your custom SQL behaviour though.

    public interface UserRepository extends JpaRepository<User, Long> {
        @Modifying
        @Query("delete from User u where u.firstName = ?1")
        void deleteUsersByFirstName(String firstName);
    }
    

    Update:

    In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove and count operations is accessible.

    public interface UserRepository extends CrudRepository<User, Long> {
    
        Long countByFirstName(String firstName);
    
        Long deleteByFirstName(String firstName);
    
        List<User> removeByFirstName(String firstName);
    
    }
    
    0 讨论(0)
  • 2020-11-28 21:33

    Derivation of delete queries using given method name is supported starting with version 1.6.0.RC1 of Spring Data JPA. The keywords remove and delete are supported. As return value one can choose between the number or a list of removed entities.

    Long removeByLastname(String lastname);
    
    List<User> deleteByLastname(String lastname);
    
    0 讨论(0)
  • 2020-11-28 21:34
    @Query(value = "delete from addresses u where u.ADDRESS_ID LIKE %:addressId%", nativeQuery = true)
    void deleteAddressByAddressId(@Param("addressId") String addressId);
    
    0 讨论(0)
  • 2020-11-28 21:38

    here follows my 2 cents. You can also use native queries, like:

    @Modifying
    @Query(value="delete from rreo r where r.cod_ibge = ?1 and r.exercicio= ?2", nativeQuery = true)
    void deleteByParameters(Integer codIbge, Integer exercicio);
    
    0 讨论(0)
  • 2020-11-28 21:39

    2 ways:-

    1st one Custom Query

    @Modifying
    @Query("delete from User where firstName = :firstName")
    void deleteUsersByFirstName(@Param("firstName") String firstName);
    

    2nd one JPA Query by method

    List<User> deleteByLastname(String lastname);
    

    When you go with query by method (2nd way) it will first do a get call

    select * from user where last_name = :firstName
    

    Then it will load it in a List Then it will call delete id one by one

    delete from user where id = 18
    delete from user where id = 19
    

    First fetch list of object, then for loop to delete id one by one

    But, the 1st option (custom query),

    It's just a single query It will delete wherever the value exists.

    Go through this link too https://www.baeldung.com/spring-data-jpa-deleteby

    0 讨论(0)
提交回复
热议问题