确实只有一次调用 但是打印的SQL是两次,两次参数完全一样 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899 Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861 Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861 Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568 Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568 Hibernate: select d.distance from distance d where d.address1_GeoX=116.672907and d.address1_GeoY=39.658568and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861 Hibernate: select d.distance from distance d where d.address1_GeoX=116.672907and d.address1_GeoY=39.658568and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861 但是打印的sql可以清晰的看到他打印了不止1次。。
public double takeDistance(double x1,double y1,double x2,double y2) {
Distance distance;
double dis = 0;
Cache cache = cacheManager.getCache("addressDistance"); Element element = cache.get(x1 + "," + y1 +","+ x2 +","+ y2);
if (element != null) {
dis = Double.parseDouble(element.getValue().toString());
} else {
// 数据库中查找
dis = distanceDAO.getDistanceBy2Coordinate(x1, y1, x2, y2);
if (dis != 0) {
cache.put(new Element(x1 + "," + y1 +","+ x2 +","+ y2, dis));
} else {
// 从百度服务器拿
dis = getDistanceFromBaidu(x1, y1, x2, y2);
distance = new Distance();
distance.setDistance(String.valueOf(dis));
distance.setAddress1GeoX(x1);
distance.setAddress1GeoY(y1);
distance.setAddress2GeoX(x2);
distance.setAddress2GeoY(y2);
distanceDAO.saveDistance(distance);
cache.put(new Element(x1 + "," + y1 +","+ x2 +","+ y2, dis));
}
}
return dis;
}下面是Dao
public double getDistanceBy2Coordinate(double x1,double y1,double x2,double y2){
double dis = 0;
String sql = "select d.distance from distance d where d.address1_GeoX="+x1+"and d.address1_GeoY="+y1+"and d.address2_GeoX="+x2+"and d.address2_GeoY="+y2;
Query query = this.getSession().createSQLQuery(sql);
if(query.list() != null && query.list().size() != 0)
dis = Double.parseDouble(query.list().get(0).toString());
return dis;
}
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.506056and d.address2_GeoY=39.623899
Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861
Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861
Hibernate: select d.distance from distance d where d.address1_GeoX=116.506056and d.address1_GeoY=39.623899and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568
Hibernate: select d.distance from distance d where d.address1_GeoX=116.567751and d.address1_GeoY=39.832861and d.address2_GeoX=116.672907and d.address2_GeoY=39.658568
Hibernate: select d.distance from distance d where d.address1_GeoX=116.672907and d.address1_GeoY=39.658568and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861
Hibernate: select d.distance from distance d where d.address1_GeoX=116.672907and d.address1_GeoY=39.658568and d.address2_GeoX=116.567751and d.address2_GeoY=39.832861
但是打印的sql可以清晰的看到他打印了不止1次。。
特么浏览器跟Hibernate有个毛线关系啊???
浏览器发送两次请求,跟Hibernate打印两遍Sql语句,有半毛钱关联么?
Element element = cache.get(x1 + "," + y1 +","+ x2 +","+ y2);
if (element != null) {
dis = Double.parseDouble(element.getValue().toString());
} else {
// 数据库中查找
dis = distanceDAO.getDistanceBy2Coordinate(x1, y1, x2, y2);楼主可以打印出这个看下element 。取得这个值一直是null