Search code examples
springhibernatemodel-view-controllerspatialgeojson

how to write hibernate spatial query?


This is homePage.jsp code :

<button id="bilgial" onclick="getVector(id)">Get Vector</button> <input type="text" id="hs">

and this is getVector(id) function :

 var id = document.getElementById("hs").value;
function getVector(id) {

    ajax({
        type: "GET",
        path: "/getGeoJson",
        data: {id: id},
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        success: function (data) {
            var parser = new OpenLayers.Format.GeoJSON();
            var feature = parser.read(data.data);
            if (feature.length != 0) {
                feature[0].attributes.id = data.id;
            }
            vectors.addFeatures(feature);
        }
    });
}

this is my controller get method :

@Controller
public class HSpatialController  {

SavegeojsonManager add = new SavegeojsonManager();

@RequestMapping(value = "/getGeoJson", method = RequestMethod.GET)
@ResponseBody
public GeoJSON getGeoJson( final HttpServletRequest request,@RequestParam("id") final String vectorId) {
    return add.get(vectorId);
}

}

and this is SavegeojsonManager class and get method :

 public GeoJSON get(String id) {

    GeoJSON geoJson = new GeoJSON();
    EntityManager em = HibernateSpatialJPA.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery("select e from SavegeojsonEntity  ????")
   query.setParameter("", ??);
    return geoJson;
}

or how can I do otherwise ?

I want to was eat with the number that was in the database that you want to take the data from the TextBox equally with data GetData ?


Solution

  • So code should be like this:

     public GeoJSON get(String id) {
            GeoJSON geoJson = new GeoJSON();
            EntityManager em = HibernateSpatialJPA.createEntityManager();
            em.getTransaction().begin();
            Query query = em.createQuery("from SavegeojsonEntity s where s.id = :id")
            query.setParameter("id", Integer.valueOf(id));
            List entityList = query.getResultList();
            if(entityList != null && !entityList.isEmpty()){
                SavegeojsonEntity entity = (SavegeojsonEntity)entityList.get(0);
                geoJson.setType(entity.getVectorType());
                if(entity.getVectorType().equals("Point")){
                    geoJson.setData(entity.getPoint());//or entity.getXXX() since you need to get point data,
                } else if(...){
                    //same for Polygon/MultiPolygon/StringLine and so on
                }
            }
            em.getTransaction().close();
            return geoJson;
     }