I am fetching data from database using hibernate. I have 3 tables in my db and i have created 3 entity classes for them. I am fetching results on the basis of two criterias profession of user and city of user.
Here is my DAO class
@SuppressWarnings({ "unchecked", "null" })
public List<User> getUsersWithSameProfessionsById(long id) throws Exception {
session = sessionFactory.openSession();
String queryString = "SELECT DISTINCT a FROM Profession p1, Profession p2, Address a JOIN p2.user u WHERE p1.u_id=:id AND p2.profession=p1.profession AND a.uid=u.id";
Query query = session.createQuery(queryString);
query.setLong("id", id);
query.setMaxResults(10);
Set<User> foo = new HashSet<User>(query.list());
Set<User> foo1 = new HashSet<User>();
Query query1=null;
if(professionList.size()<10){
String queryString1 = "SELECT DISTINCT a FROM Address c1, Address c2, Address a JOIN c2.user u WHERE c1.uid=:id AND c2.city=c1.city AND a.uid=u.id";
query1 = session.createQuery(queryString1);
query1.setLong("id", id);
foo1.addAll(query1.list());
}
List<User> professionList3 = new ArrayList<User>();
Set<User> foo2 = new HashSet<User>();
foo2.addAll(foo);
foo2.addAll(foo1);
professionList3.addAll(foo2);
return professionList3;
}
Here i am fetching records from first query on the basis of profession but if results are less than 10 then i am running second query on the basis of city. But if results are matching with both criterias then i am getting duplicate results and i dont want it..i want if results have been come in first query then those results not come in second query. I am new in hql queries so please help me.
Thanx in advance