Hello
I'm having a problem making the right restrictions for my database seach using Hibernate. I'm having two tables being Location and Keyword, and i want to seach for locations in an area that also holds 1 or more of my keywords, and store those locations in a list.
Is it possible to make a "Criteria crit = session.createCriteria(Location.class);" that somehow also includes "Criteria crit = session.createCriteria(Keyword.class);
Location and Keyword is related through another another class named Company.
appreciating the help.
Code:
public Vector<Location> getCoordinatesOfLocations(Double xMin, Double xMax, Double yMin, Double yMax, ArrayList<String> keywords)
{
Vector<Location> list = new Vector<Location>();
xMin = (coordinates.getLngMin());
xMax = (coordinates.getLngMax());
yMin = (coordinates.getLatMin());
yMax = (coordinates.getLatMax());
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
//Criteria Query Example
Criteria crit = session.createCriteria(Location.class);
crit.add(Restrictions.between("x", new BigDecimal(xMin), new BigDecimal(xMax))); //Between condition
crit.add(Restrictions.between("y", new BigDecimal(yMin), new BigDecimal(yMax))); //Between condition
crit.add(Restrictions.or(Restrictions.like("keyword", "%"+keywords.get(0)+"%"),
Restrictions.or(Restrictions.like("keyword", "%"+keywords.get(1)+"%"), //currently only taking 3 keywords.
(Restrictions.like("keyword", "%"+keywords.get(2)+"%")))));
List locations = crit.list();
for(Iterator it = locations.iterator();it.hasNext();){
Location location = (Location) it.next();
list.add(location);
}
session.close();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
}
return list;
}