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.
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. ..
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