Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Criteria - Fetching results from two tables without any association
    text
    copied!<p>I have been trying hard to implement a functionality of Search using Hibernate criteria. The requirement is bit vague. User could enter any text/word in the search field, the search has to be made through multiple tables which doesn't have any association between each other. </p> <p>following are the two entity classes. </p> <pre><code>Table A (EntityClassA.java) Table B (EntityClassB.java) </code></pre> <p>if a user enter "anycharecter" that should be searched in those two tables and should return fetched list(s) matching the entered character(s). <em>Also I want to store results from table A to List and result from Table B to List</em>.</p> <p>I am not sure how this can achieved using hibernate criteria. </p> <p>I could only manage to write this for one table. </p> <pre><code>public List&lt;EntityClassAVo&gt; search((string keywords){ List&lt;EntityClassAVo&gt; outputList = new ArrayList&lt;EntityClassVo&gt;(); Criteria criteria = etSessionFactory().getCurrentSession().createCriteria(EntityClassA.class); //applying restriction - as I need to search entered string in all columns. criteria.add(Restrictions.ilike("id", keywords, MatchMode.ANYWHERE)); criteria.add(Restrictions.ilike("name", keywords, MatchMode.ANYWHERE)); criteria.add(Restrictions.ilike("age", keywords, MatchMode.ANYWHERE)); outputList = criteria.list(); return outputList; } </code></pre> <p><strong>Question</strong></p> <p>1) if its possible, How to write criteria for EntityClassB.java in the same "search" method?</p> <p>2) if question 1 is possible, how to store results fetched from EntityClassA in one object and EntityClassB in another object?</p> <p>3) If the above is not simple possible, i am probably going to ask to change the design ( only if, If i have a strong reason). - I am thinking of putting a intermediate table (Table X) between (Table A) and (Table B).</p> <p><strong>[EDITED]</strong> 4) There is a scenario, where user wants to search results from either of 1 table or both. How we are going to check this condition?</p> <p>Please help me to crack this thing. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload