<p>I'm trying to do a basic "OR" on three fields using a hibernate criteria query.</p>

<p>Example</p>

<pre><code>class Whatever{
 string name;
 string address;
 string phoneNumber;
}
</code></pre>

<p>I'd like to build a criteria query where my search string could match "name" or "address" or "phoneNumber".</p>

[{'answer_id': 57526, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 8, 'selected': True, 'text': '<p>You want to use <code>Restrictions.disjuntion()</code>. Like so</p>\n\n<pre><code>session.createCriteria(Whatever.class)\n    .add(Restrictions.disjunction()\n        .add(Restrictions.eq("name", queryString))\n        .add(Restrictions.eq("address", queryString))\n        .add(Restrictions.eq("phoneNumber", queryString))\n    );\n</code></pre>\n\n<p>See the Hibernate doc <a href="http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-narrowing" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 57541, 'author': 'Rob Oxspring', 'author_id': 1867, 'author_profile': 'https://Stackoverflow.com/users/1867', 'pm_score': 6, 'selected': False, 'text': '<p>Assuming you have a hibernate session to hand then something like the following should work:</p>\n\n<pre><code>Criteria c = session.createCriteria(Whatever.class);\nDisjunction or = Restrictions.disjunction();\nor.add(Restrictions.eq("name",searchString));\nor.add(Restrictions.eq("address",searchString));\nor.add(Restrictions.eq("phoneNumber",searchString));\nc.add(or);\n</code></pre>\n'}, {'answer_id': 58643, 'author': 'Geir-Tore Lindsve', 'author_id': 4582, 'author_profile': 'https://Stackoverflow.com/users/4582', 'pm_score': 2, 'selected': False, 'text': '<p>Just in case anyone should stumble upon this with the same question for NHibernate:</p>\n\n<pre><code>ICriteria c = session.CreateCriteria(typeof (Whatever))\n    .Add(Expression.Disjunction()\n        .Add(Expression.Eq("name", searchString))\n        .Add(Expression.Eq("address", searchString))\n        .Add(Expression.Eq("phoneNumber", searchString)));\n</code></pre>\n'}, {'answer_id': 25283984, 'author': 'Dharmender Rawat', 'author_id': 3936966, 'author_profile': 'https://Stackoverflow.com/users/3936966', 'pm_score': 4, 'selected': False, 'text': '<pre><code>    //Expression :  (c1 AND c2) OR (c3)      \n\n\n     Criteria criteria = session.createCriteria(Employee.class);\n\n      Criterion c1 = Restrictions.like("name", "%e%");\n      Criterion c2 = Restrictions.ge("salary", 10000.00);\n      Criterion c3 = Restrictions.like("name", "%YYY%");\n      Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3);\n      criteria.add(c4);\n</code></pre>\n\n<p>//Same thing can be done for (c1 OR c2) AND c3, or any complex expression.</p>\n'}, {'answer_id': 25294252, 'author': 'Dharmender Rawat', 'author_id': 3936975, 'author_profile': 'https://Stackoverflow.com/users/3936975', 'pm_score': 3, 'selected': False, 'text': '<pre><code>//Expression :  (c1 AND c2) OR (c3)      \n\n\n Criteria criteria = session.createCriteria(Employee.class);\n\n  Criterion c1 = Restrictions.like("name", "%e%");\n  Criterion c2 = Restrictions.ge("salary", 10000.00);\n  Criterion c3 = Restrictions.like("name", "%YYY%");\n  Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3);\n  criteria.add(c4);\n\n  //Same thing can be done for (c1 OR c2) AND c3, or any complex expression.\n</code></pre>\n'}, {'answer_id': 36494224, 'author': 'Tiago Medici', 'author_id': 6117311, 'author_profile': 'https://Stackoverflow.com/users/6117311', 'pm_score': 1, 'selected': False, 'text': '<p>The conditions can be applied using the or / and in different levels of the query\nusing disjunction</p>\n\n<pre><code>Criteria query = getCriteria("ENTITY_NAME");\nquery.add(Restrictions.ne("column Name", current _value));\n\nDisjunction disjunction = Restrictions.disjunction();\n\nif (param_1 != null)\n    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param1)));\n\nif (param_2 != null)\n    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_2)));\n\nif (param_3 != null)\n    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_3)));\nif (param_4 != null &amp;&amp; param_5 != null)\n    disjunction.add(Restrictions.or(Restrictions.and(Restrictions.eq("column Name", param_4 ), Restrictions.eq("column Name", param_5 ))));\n\nif (disjunction.conditions() != null &amp;&amp; disjunction.conditions().iterator().hasNext())\n    query.add(Restrictions.and(disjunction));\n\nreturn query.list();\n</code></pre>\n'}, {'answer_id': 56698228, 'author': 'ronak', 'author_id': 6673843, 'author_profile': 'https://Stackoverflow.com/users/6673843', 'pm_score': 1, 'selected': False, 'text': '<p>This is what worked for me for an OR condition, that too with an IN condition and not the answer up-voted most on this discussion:</p>\n\n<pre><code>criteria.add( Restrictions.or(\n                    Restrictions.eq(ch.getPath(ch.propertyResolver().getXXXX()), "OR_STRING"),\n                        Restrictions.in(ch.getPath(ch.propertyResolver().getYYYY()), new String[]{"AA","BB","CC"})\n                    ));\n</code></pre>\n\n<p><strong>Resulting Query:</strong></p>\n\n<pre><code>  and (\n            this_.XXXX=? \n            or this_.YYYY in (\n                ?, ?, ?\n            )\n        ) \n</code></pre>\n'}, {'answer_id': 63181989, 'author': 'Gaspar', 'author_id': 3681565, 'author_profile': 'https://Stackoverflow.com/users/3681565', 'pm_score': 1, 'selected': False, 'text': '<p>If someone is using CriteriaQuery instead of Criteria, you can put all your expressions in a <code>Predicate</code> list and put a OR by predicates size like this:</p>\n<pre><code>List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();\nif (...) {\n  predicates.add(...);\n}\n\ncriteriaQuery.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/57484', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1310/']