如何做休眠空间删除?

How to do hibernate spatial delete?

我想删除我的数据库数据。

这是 AJAX 代码,取自 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);
        }
    });
}

这是我的按钮代码:

  • 删除
  • 这是我的控制器(get 方法)

     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);
    }
    

    这是 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;
    }
    

    }

    这是 GeoJSON Class :

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

    首先,当您删除实体时,您不能 return 在您的示例中删除该实体的实例。

    你可以这样做: 在您的视图中,例如在 jsp 中,您可以显示已通过 Controller 发送的消息。可能在 ajax 请求中 success 的一部分。

    控制器代码:

    @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;
    }
    

    你的dao代码: 这可以通过多种方式完成:

     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;
    }