0

I'm trying to delete a object in Hibernate and for some reason its not getting deleted..

Now, I want to enable the DefaultPersistEventListener and really want to understand what is the problem but im not sure how to do it?

Emp1000 e1 = new Emp1000();
e1.setId(1067);
session.delete(e1);
System.out.println("delete over");

Employee table

@Entity
@NamedQuery(name = "Emp1000.findAll", query = "SELECT e FROM Emp1000 e")
public class Emp1000 implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String firstName;

    private String lastName;

    // bi-directional many-to-one association to EmpDept
    // @OneToMany(mappedBy="emp1000")
    // @OneToMany(mappedBy="emp1000", cascade = CascadeType.MERGE)// Cascade merge and you need to explicitly save it
    @OneToMany(mappedBy = "emp1000", cascade = CascadeType.ALL) // Cascade merge and you need to explicitly save it
    @Fetch(FetchMode.SELECT) //Lazy loading
    //@Fetch(FetchMode.JOIN)
    private List<EmpDept> empDepts;

    public Emp1000() {
    }

    public int getId() {
        return this.id;
    }

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

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<EmpDept> getEmpDepts() {
        return this.empDepts;
    }

    public void setEmpDepts(List<EmpDept> empDepts) {
        this.empDepts = empDepts;
    }

    public EmpDept addEmpDept(EmpDept empDept) {
        getEmpDepts().add(empDept);
        empDept.setEmp1000(this);

        return empDept;
    }

    public EmpDept removeEmpDept(EmpDept empDept) {
        getEmpDepts().remove(empDept);
        empDept.setEmp1000(null);

        return empDept;
    }

}

Emp_Dept

@Entity
@Table(name="emp_dept")
@NamedQuery(name="EmpDept.findAll", query="SELECT e FROM EmpDept e")
public class EmpDept implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String dept;

    //bi-directional many-to-one association to Emp1000
    @ManyToOne
    @JoinColumn(name="emp_id")
    private Emp1000 emp1000;

    public EmpDept() {
    }

    public int getId() {
        return this.id;
    }

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

    public String getDept() {
        return this.dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public Emp1000 getEmp1000() {
        return this.emp1000;
    }

    public void setEmp1000(Emp1000 emp1000) {
        this.emp1000 = emp1000;
    }

}
user1050619
  • 19,822
  • 85
  • 237
  • 413

1 Answers1

0

First you need to make sure your code execute inside the transaction and commit after that. If that is, use session.flush() to synchronized system state with database.

Details are explaining in this thread. Question about Hibernate session.flush()

Community
  • 1
  • 1
zawhtut
  • 8,335
  • 5
  • 52
  • 76