Create jpa search function

Asked 2 years ago, Updated 2 years ago, 115 views

I want to type my name in the search bar and display the data in the table in view, but it doesn't work.

Example) Hit "Tanaka" and "Tamura" by typing "Ta"

I try to get arguments dynamically using the setParameter method in createNamedQuery.
Unable to retrieve more than one record.

Example) If you type "ta", the error "Operand should contain 1 column(s)" appears.

How do I retrieve multiple records in an ambiguous search?

Query statement

query="SELECTR FROM Report ASr WHEREer.employee=:employee"
query="SELECTE FROM Employee ASE WHERE e.name LIKE:name"

Controller

String employee_name =request.getParameter("search");

        // Store query results in variables and retrieve records with the appropriate name
        try {List<Employee>employee_search=em.createNameQuery("getEmployeesName",Employee.class)
                .setParameter("name", employee_name+"%")
                .getResultList();

        System.out.println(employee_search);

        // Retrieving data in the report table
        List<Report>r_employee_id=em.createNameQuery("getCreateUser", Report.class)
                .setParameter ("employee", employee_search)
                .getResultList();
        em.close();

java jpa

2022-09-30 21:43

1 Answers

The quick answer is that the first SQL (getEmployeesName) can retrieve multiple records, while the second SQL (getCreateUser) with those records as a search criteria is a WHERE clause (compared to =.Therefore,

query="SELECTR FROM Report ASr WHEREer.employee=:employee"

where

query="SELECTR FROM Report ASr WHEREER.employee IN:employee"

If you don't do this, it won't work.


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.