1

I have a sql questions table. The table accordingly contains Questions objects. Each object contains the question and answer strings.

package com.example.kr2db.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class Question {
    @Id
    @GeneratedValue
    private int id;
    private String question;
    private String answer;

    public Question() {
    }

    public Question(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }

In the repository interface, I need a method that will search the table for a Question object containing a word or phrase from the question or answer string. How do I do this?

I use List findQuestionByQuestionContainsIgnoreCase(String question); but it's not exactly what I need. Because let's say the word "rat" is present in the table and in the question line, but if I write "rats", then such a query does not work.

package com.example.kr2db.repository;

import com.example.kr2db.model.Question;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface QuestionRepository extends JpaRepository<Question, Integer> {
    List<Question> findQuestionByQuestionContainsIgnoreCase(String question);
}
mahfuj asif
  • 1,691
  • 1
  • 11
  • 32
Stasis2
  • 11
  • 1
  • Can you try `findByQuestionContainingOrAnswerContaining(String question) ` – mahfuj asif Jul 24 '23 at 04:45
  • You need to “stem” your search term. But are you happy for “rat” to match “crates”? – tgdavies Jul 24 '23 at 04:48
  • The application, unfortunately, does not start with the request that you suggest. I need to be able to find not only words that exactly match those that are present in the Question object, but also words with the same root. For example, you don't remember the exact word, but you do remember something similar. This is exactly the kind of search I am trying to implement, as far as possible using JpaRepository methods – Stasis2 Jul 24 '23 at 10:44

0 Answers0