java - 如何使用 .remove 和 .flush 从数据库中删除最后一行

标签 java mysql database flush

我必须使用 .remove 和 .flush 从数据库(使用 MySQL 创建)中删除最后一行,但它一直告诉我该行未删除。我试图用不同的方式编写它,但我似乎无法让它工作。任何帮助,将不胜感激!这是代码:

package com.vz.test.mbean;

import com.vz.test.db.Person;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@ManagedBean
@ViewScoped
public class AddPerson {

    com.vz.test.db.Person person = null;
    private int identification = 0;
    private String name = "";
    private String age = "";
    private String telephone = "";
    private String results="";
    private java.util.List<Person> alst = new java.util.ArrayList<Person>();

    public java.util.List<Person> getAlst() {
        return alst;
    }

    public void setAlst(ArrayList<Person> alst) {
        this.alst = alst;
    }


    public String getResults() {
        return results;
    }

    public void setResults(String results) {
        this.results = results;
    }




    @PersistenceContext(unitName = "WebApplication2PU")
    private EntityManager em;
    @Resource
    private javax.transaction.UserTransaction utx;

    public int getIdentification() {
        return identification;
    }

    public void setIdentification(int identification) {
        this.identification = identification;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }


    public AddPerson() {
    }

    public void fetchPersonList(){
     Query qu = em.createQuery("SELECT a FROM Person a");  
     alst= (java.util.List<Person>) q.getResultsList();
        System.out.println("alst.size():"+ alst.size());
    }

    public void addRow() {
        try {
            System.out.println("I am in addRow");
            com.vz.test.db.Person person = new com.vz.test.db.Person();
            person.setName(name);
            person.setAge(age);
            person.setTelephone(telephone);
            persist(person);
            result="Row is added";

        }
        catch (Exception e) {
            result="Row is NOT added";
        }
    }

    public void deleteRow(){

        try{
        em.remove(person);
        em.flush();
        persist(person);
        result="Row is deleted";
        }
        catch (Exception e){
            result="Row is NOT deleted";
        }

    }

    public void persist(Object object) {
        try {
            utx.begin();
            em.persist(object);
            utx.commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            throw new RuntimeException(e);
        }
    }
}

最佳答案

em.remove(person);
em.flush();
persist(person);

您首先删除该实体,然后尝试保留它。将删除调用包装在用户事务中,这应该可以解决问题:

    try {
      if (person != null) {
        utx.begin();
        em.remove(person);
        utx.commit();
        result="Row is deleted";
      } else {
        System.out.println("The field 'person' is null. Can't remove anything");
      }
    }
    catch (Exception e) {
      // TODO add proper exception handling/logging 
      result="Row is NOT deleted";
    }

关于java - 如何使用 .remove 和 .flush 从数据库中删除最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17406112/

相关文章:

mysql - 查找特定日期之后的新值

php - Symfony 2 - Doctrine 2 - Native Sql - 删除查询

php - 订单的数据库结构

java - Hibernate 删除不会触发

java - 如何在Jpanel上绘画?

java - 如何在java中为列表分配对象类型?

mysql - 将表名添加到列会返回未知列错误

java - Spring jpa多对多惰性删除

mysql - 使我的 mysql 查询更加高效

java - GroovyScriptEngine : load groovy scripts from subfolder