Rheza001
Rheza001

Reputation: 79

Jpa repository findBy method signature with multiple or operators?

I have Entity class for Product like this:

@Entity
public class Product {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer ID_product;

  @NotNull
  private String prodName;

  @NotNull
  private String prodDesc;

  @NotNull
  private Integer price;

  @NotNull
  private String prodBrand;

  @NotNull
  private String prodImage;

  @ManyToOne
  private Category category;

I want to make the controller look like this:

List<Product> productList = productRepository.findByProdNameOrProdBrandContains(search);
model.addAttribute("products" , productList);

How do I wrote findBy methods in my Repository if i want to search the keyword of prodName OR prodBrand? I'm imagining something like this:

List<Product> findByProdNameOrProdBrand(String prodName || String prodBrand);

Do I have to create different function/ model or something, and if so, how can i use both fucntion/ model in my thymeleaf? Any help is appreciated. Thank you in advance.

Upvotes: 1

Views: 2517

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Just create the following method in repository:

List<Product> findByProdNameOrProdBrand(String prodName, String prodBrand);

But I suggest you to switch to Sepcification<T> when query with more than 3 conditions.

Upvotes: 1

Related Questions