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

时间:2013-07-01 13:51:17

标签: 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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

首先删除实体,然后尝试保留它。相反,在用户事务中包装remove调用,这应解决问题:

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