Search code examples
hibernatepostgresqlcascadecascading-deleteshibernate-spatial

How to do hibernate spatial delete?


I want to delete my database datas.

This is AJAX code taken from value from the TextBox :

 function deleteVector()
{
    var value = $('#hs').val();
    console.log(value);
    $.ajax({
        type: "GET",
        url: "/deleteGeoJson",
        data: {id: value},
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        success: function (data) {
            var parser = new OpenLayers.Format.GeoJSON();
            var feature = parser.read(data.data);
        }
    });
}

this is my button code :

  • Delete
  • and this is my controller ( get method )

     SavegeojsonManager add = new SavegeojsonManager();
    
     @RequestMapping(value = "/deleteGeoJson", method = RequestMethod.GET)
    @ResponseBody
    public GeoJSON deleteGeoJson( final HttpServletRequest request,@RequestParam("id") final int vectorId) {
        return add.delete(vectorId);
    }
    

    this is SavegeojsonManager class codee :

     public GeoJSON delete(int id) {
    
        GeoJSON geoJson = new GeoJSON();
        try{
            EntityManager em = HibernateSpatialJPA.createEntityManager();
            em.getTransaction().begin();
            //How can I continue ?
           //If there's one thing if it is connected to the data you delete it
          //so cascade ??
    
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return geoJson;
    }
    

    }

    and this is GeoJSON Class :

    private int id;
     public int getId() { return id; }
    public void setId(int id) {  this.id = id; }
    

    Solution

  • First of all when you are deleting entity you cant return instance of that deleted entity in you example.

    You could do in this way: In you your view for example in jsp you can show that message that has been sent via Controller. Propably in ajax request from success part of it.

    Controller code:

    @RequestMapping(value = "/deleteGeoJson", method = RequestMethod.GET)
    @ResponseBody
    public String deleteGeoJson( final HttpServletRequest request,@RequestParam("id") final int vectorId) {
        String message = null;
        boolean deleted = add.delete(vectorId);
        if(deleted){
            message = "Geo object has been deleted with id: "+vectorId;
        } else {
            message = "There was some problem with deleting GeoObject"
        }
        return message;
    }
    

    Your dao code: This can be done in many ways:

     public boolean delete(int id) {
     boolean deleted = false;
     try{
        EntityManager em = HibernateSpatialJPA.createEntityManager();
        em.getTransaction().begin();
        Query query = em.createQuery("delete SavegeojsonEntity s where s.id = :id");
        query.setParameter("id", id);
    
        int result = query.executeUpdate();
        em.getTransaction().commit();
        if(result > 0){
            deleted = true;
        }
    
     } catch (Exception ex){
        ex.printStackTrace();
     }
    return deleted;
    }