Hibernate Restrictions / Criteria Beginner Question

Deadly 提交于 2021-02-10 03:57:40

问题


Hi I have been trying to work out the best way to do this today to no avail.

What I would ideally like to do is to create an alias distance calculated by the SQL formula below (although I am open to other ways of calculating the distance, this was just the way that seemed it should be easiest)

Once I have that alias I want to be able to use it in a Restrictions fashion to find all that are within a certain distance.

I would also like to be able to sort via distance.

This is part of a bigger search criteria that is built up so I would ideally like to keep using Criteria. ( I already limit the range of Lat and Long values to make the distance calculation required on less fields.

Criteria criteria = session.createCriteria(Activity.class)
       .createAlias("activityLocations", "actloc")
       .createAlias("actloc.location", "location")
       .createAlias("location.address", "addr");
       criteria.add((Restrictions.and(Restrictions.between("addr.latitude", latmin,     
       latmax),Restrictions.between("addr.longitude", truelongmin, truelongmax))));


       String sql =  "SQRT( POW( 69.1 * ( addr3_.latitude - " + point[1]     
       +" ) , 2 ) + POW( 69.1 * ( "+point[0] +" - addr3_.longitude ) * COS( addr3_.latitude /"
       +" 57.3 ) , 2 ) )  < "+distance;    
       criteria.add(Restrictions.sqlRestriction(sql));

Currently I have addr3_ in the sql query because I was looking at the verbose output and that is the way that Hibernate has generated the query (this hack worked in the one instance i was looking at but I dread to think about the longer term implications so would not want it to stay there!!)


回答1:


In SQL restrictions you can refer to the alias of the criteria root as {alias}. To use it in this case, you need a "sub criteria" rooted at location.address:

Criteria criteria = session.createCriteria(Activity.class) 
       .createAlias("activityLocations", "actloc") 
       .createAlias("actloc.location", "location");

Criteria addr = criteria.createCriteria("location.address"); 
addr.add((Restrictions.and(Restrictions.between("latitude", latmin,      
       latmax), Restrictions.between("longitude", truelongmin, truelongmax)))); 

String sql =  "SQRT( POW( 69.1 * ( {alias}.latitude - " + point[1]      
       +" ) , 2 ) + POW( 69.1 * ( "+point[0] +" - {alias}.longitude ) * COS( {alias}.latitude /" 
       +" 57.3 ) , 2 ) )  < "+distance;     
addr.add(Restrictions.sqlRestriction(sql)); 


来源:https://stackoverflow.com/questions/4136321/hibernate-restrictions-criteria-beginner-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!