Hibernate like criteria for more than one rows

时间:2016-09-01 06:22:05

标签: hibernate hibernate-criteria

Hi i'm using Hibernate criteria for fetching data from database.The code is given below.I want to search a string with two columns. The below code is only checking on one column , How do criteria like with two columns.

    Criteria c = getSession().createCriteria(ActivatedUser.class);
                    c.createAlias("key", "key");
c.createAlias("installedUser", "installedUser");
                        if (searchTerm != null && !searchTerm.isEmpty()) {
    //here i want to check 2 columns for a match
                        c.add(Restrictions.like("key.keyCode", searchTerm.toUpperCase() + "%"));
                        c.add(Restrictions.like("installedUser.email", searchTerm.toUpperCase() + "%"));
                    }

1 个答案:

答案 0 :(得分:1)

您可以使用逻辑AND运算符

c.add(Restrictions.and(Restrictions.like("key.keyCode", searchTerm.toUpperCase() + "%"), Restrictions.like("installedUser.email", searchTerm.toUpperCase() + "%")));