[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?

Overview

As described in the article [Spring Data JPA] Naming rules for automatically implemented methods, JPA defines methods that match the repository naming rules. So, there is a function that automatically generates a query. This time, we will talk about whether the And condition can be used at the time of delete in the automatic generation.

About delete

Conclusion

And condition was usable. I haven't tried it, but I think other conditions can probably be used. However, I think it is better to implement complicated conditions with NativeQuery. ..

Implementation sample

Entity

UserPost.java



@Entity
@Table(name = "user_posts")
public class UserPost {
  @Id
  private long id;
  private long userId;
  private String contents;

  public long getId() {
    return this.id;
  }
  public void setId(long id) {
    this.id = id;
  }

  public long getUserId() {
    return this.userId;
  }
  public void setUserId(long userId) {
    this.userId = userId;
  }

  public String getContents() {
    return this.contents;
  }
  public void setContents(String contents) {
    this.contents = contents;
  }
}

Repository

UserPostRepository.java



@Repository
public interface UserPostRepository extends JpaRepository<UserPost, String> {
    //Delete record by specifying id and userId with And condition
    void deleteByIdAndUserId(long id, long userId);
}

Recommended Posts

[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
[Spring Boot] List of validation rules that can be used in the property file for error messages
[Order method] Set the order of data in Rails
[For beginners] DI ~ The basics of DI and DI in Spring ~
Make the where clause variable in Spring Data JPA
The story that the port can no longer be used in the Spring boot sample program
[Question] Can nullif be used in the count function in JPQL?
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
Spring Data JPA: Write a query in Pure SQL in @Query of Repository
Summary of ORM "uroboroSQL" that can be used in enterprise Java
How to delete large amounts of data in Rails and concerns
Spring validation was important in the order of Form and BindingResult
See the behavior of entity update with Spring Boot + Spring Data JPA
About the range and scope where Day16 variables can be used
[Firestore] Extract the collection with where condition in Ruby and delete the record
[Rails] "pry-rails" that can be used when saving with the create method
How to change the maximum and maximum number of POST data in Spark
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
I separated the same description as a method under private in Rails controller and implemented it so that it can be called by before_action
Exists using Specification in Spring Data JPA
I received the data of the journey (diary application) in Java and visualized it # 001
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Be absolutely careful when putting the result of and / or in a variable!
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
[Java] Handling of JavaBeans in the method chain
[Rails 6] method :: delete cannot be used with link_to
Spring Data JPA Entity cross-reference and its notes
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
[Swift] The process of registering the item selected in "UISegmentedControl" in Realm and displaying the registration data.