Question

I'm trying to use HQL using LIKE, but I want to use multiple columns

Like this one but not only name field, lets take for example name and address dateOfInscription

String query = "from user u where u.name like :name"
getHibernateTemplate().findByNamedParam(query, "name", '%' + str + '%');
Was it helpful?

Solution 2

I did that this way:

        String query = ""
                + "from User where "
                + "u.firstName like '%" + firstName+ "%' ";
        if (!"".equals(lastName)) {
            query += "and u.lastName like '%" + lastName + "%' ";
        }
        if (!"".equals(bDate)) {
            query += "and u.bDate like '%" + bDate + "%'";
        }
        List<User> resultList = null;
        resultList = hibernateTemplate.find(query);

OTHER TIPS

I am guessing you are doing a "search" functionality and want to search multiple columns based on user input. The following should work.

String query = "from user u where u.name like :name OR u.address like :address OR u.dateOfInscription like :dateOfInscription"
getHibernateTemplate().findByNamedParam(query, "name", '%' + str + '%', "address", '%' + str + '%', "dateOfInscription", '%' + str + '%');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top