Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JpaRepository difference between deleteBy and removeBy

I have been testing out the delete feature for JpaRepository and have this (simplified) Interface:

@Repository
public interface BrowserInfoDao extends JpaRepository<BrowserInfo, String> {

    @Transactional
    public Integer deleteByClientGuidAndBrowserGuid(String clientGuid, String browserGuid);

    @Transactional
    public Integer removeByClientGuidAndBrowserGuid(String clientGuid, String browserGuid);

}

In my tests I can't find any behavioral difference between the removeBy and deleteBy method. So my question is what is the difference between these if any? Is there a best practice reason to use one over the other?

Note: I am using Hibernate as the implementation.

like image 967
DavidR Avatar asked Dec 13 '15 03:12

DavidR


People also ask

What is the difference between a CrudRepository and a JpaRepository What is the difference between a CrudRepository and a JpaRepository?

Each of these defines its own functionality: CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Why do we use JpaRepository?

JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.


1 Answers

They are the same . Which one to choose depend on your personal preference.

For detail , please see this. Code-wise, check out PartTree which defines the following regex pattern for matching these delete query:

private static final String DELETE_PATTERN = "delete|remove";
private static final Pattern PREFIX_TEMPLATE = Pattern.compile( //
        "^(" + QUERY_PATTERN + "|" + COUNT_PATTERN + "|" + DELETE_PATTERN + ")((\\p{Lu}.*?))??By");
like image 184
Ken Chan Avatar answered Sep 28 '22 04:09

Ken Chan