What is different between Query keywords Containing, IsContaining, Contains in Spring Data ?
List<Movie> findByTitleContaining(String title);
List<Movie> findByTitleContains(String title);
List<Movie> findByTitleIsContaining(String title);
What is different between Query keywords Containing, IsContaining, Contains in Spring Data ?
List<Movie> findByTitleContaining(String title);
List<Movie> findByTitleContains(String title);
List<Movie> findByTitleIsContaining(String title);
Containing: Used for substring searches within string fields. It translates to the SQL LIKE operator with wildcards (%substring%).
Contains: Used for checking membership within collection fields (e.g., List, Set). It is useful for querying if a collection contains a specific element.
On the other hand both Containing and isContaining serve the same purpose and are interchangeable in terms of functionality. The choice between them depends on which naming convention fits better with your method naming style and improves code readability.
----- Containing Example -----
Entity
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
Repository:
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByNameContaining(String namePart);
//or
List<User> findByNameIsContaining(String namePart);
}
----- Contains Example -----
Entity
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany
private Set<Author> authors;
// getters and setters
}
Repository:
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByAuthorsContains(Author author);
}
© 2022 - 2024 — McMap. All rights reserved.