简体   繁体   English

HQL删除在Java Servlet中不起作用

[英]HQL delete not working in a java servlet

I am battling with an HQL delete query.I have a custom jsp that displays some records from the database according to certain criteria. 我正在与HQL删除查询作斗争。我有一个自定义的jsp,可根据某些条件显示数据库中的一些记录。 My attempt is to mark a certain number of rows displayed in a custom jsp from the database. 我的尝试是标记来自数据库的定制jsp中显示的一定数量的行。 I can mark the records and correctly read their row id's (I have tested that). 我可以标记记录并正确读取其行ID(我已经测试过)。 So I am attempting to delete the marked rows using HQL in custom servlet but the records are not deleted I am not sure if there is a need for extra configuration on the persistence file. 因此,我试图在自定义servlet中使用HQL删除标记的行,但记录并未删除。我不确定是否需要对持久性文件进行额外的配置。 Any contribution about HQL DELETE or UPDATE in a servlet is welcome my code for the servlet is as below 欢迎对servlet中的HQL DELETE或UPDATE做出任何贡献,我的servlet代码如下

package net.billing.deleterecords;

import java.io.*;
import javax.persistence.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.openxava.jpa.*;

@WebServlet("/deleteDiaryServlet")
public class deleteDiaryServlet extends HttpServlet{

    public void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException,IOException{

        String[] marked_records = request.getParameterValues("marked_record");

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        out.print("<html><body>");
        out.print("The deleted records are: ");
        out.print("<ul>");
        for(String selected:marked_records){
            out.print("<li>" + selected + "</li>");
                out.print("<li>" + selected+  "</li>");
            String oid = "8a4b301f5b142791015b142c13c50002";
            Query query = XPersistence.getManager()
                        .createQuery("delete from Diary diary where diary.oid=:oid");
               query.setParameter("oid",oid);
               int count= query.executeUpdate();
               System.out.println("Rpws affected"+count);
        }

        out.print("</ul>");
        out.print("</body></html>");

    }
}

You will need to commit your transaction. 您将需要提交交易。 Try adding 尝试添加

XPersistence.commit()

after your for loop, so that all delete transactions will get committed together. 在您的for循环之后,以便所有删除事务将一起提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM