gpt4 book ai didi

java - 删除实体时 hibernate 分离实体错误

转载 作者:行者123 更新时间:2023-11-30 10:31:01 25 4
gpt4 key购买 nike

根据 here只要我使用 org.hibernate.Session而不是 JPA , 应该允许使用 session.remove(entity) 删除分离的实体

但是,每当我尝试删除一个实体时,由于处于分离状态,它不允许我将其删除。

线路是这样的;

employeeDao.delete(employee)

即使我尝试这样删除

employeeDao.delete(employeeDao.findById(employee.getId)):

它给出了同样的错误并且没有删除它。

异常(exception)的是

Unexpected RuntimeException Last cause: Removing a detached instance rd.core.model.Employee#69

堆栈跟踪是

Root cause:

java.lang.IllegalArgumentException: Removing a detached instance rd.core.model.Employee#69 at org.hibernate.jpa.event.internal.core.JpaDeleteEventListener.performDetachedEntityDeletionCheck(JpaDeleteEventListener.java:69) at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:106) at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:73) at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:920) at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:896) at rd.core.persistence.AbstractHibernateDao.delete(AbstractHibernateDao.java:35) at rd.core.services.EmployeeManagerImpl.deleteEmployee(EmployeeManagerImpl.java:186) at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:66) at java.lang.reflect.Method.invoke(Method.java:606)

我想我搞砸了一些完全错误的事情。即使这会导致相同的异常。

 public void remove(Employee employee) {
getCurrentSession().merge(employee);
getCurrentSession().delete(employee);
}

这是我的POJO

@Entity
@Table(name = "TMP_EMPLOYEE")
public class Employee implements Identifiable<Integer> {

private static final String ID_GENERATOR_NAME = "TMP_EMPLOYEE_ID_SEQ";
public static final String PROP_FULL_NAME = "fullName";
public static final String PROP_ID = "id";
public static final String PROP_DEP = "department";

public static final String COLUMN_FULL_NAME = "FULL_NAME";
public static final String COLUMN_ID = "emp.ID";
public static final String COLUMN_DEP = "NAME";

@Column(name = "ID", nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_GENERATOR_NAME)
@SequenceGenerator(name = ID_GENERATOR_NAME, sequenceName = "S_EMPLOYEE_ID", allocationSize = 1)
private Integer id;

@Column(name = "FULL_NAME", nullable = false, length = 60)
@Basic
private String fullName;

@ManyToOne
@JoinColumn(name = "DEP_ID", referencedColumnName ="ID", nullable = false)
@Fetch(FetchMode.SELECT)
private Department department;

public Integer getId() {
return id;
}

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

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public Integer getDepartmentId() {
return 0;
}

public void setDepartmentId(Integer departmentId) {

}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Employee))
return false;

Employee that = (Employee) o;

if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null)
return false;

return true;
}

@Override
public int hashCode() {
return getId() != null ? getId().hashCode() : 0;
}

@Override
public Integer getIdentity() {
return id;
}

@Override
public String toString(){
return "Id: " + this.id + " Employee Name: " + this.fullName;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

这听起来很简单,但到底是什么原因导致实体状态发生这样的变化,为什么我不能将其删除,即使它已分离?

注意:我通过类委托(delegate)同一个员工实例。所以它总是同一个实体。

如果您需要更多数据,请索取。我实际上不知道哪些数据可以帮助弄清楚这一点。

最佳答案

您可以阅读有关“why can't I remove it even if it is detached”的内容,在那里您可以找到:

It is important to note that Hibernate itself can handle deleting detached state. JPA, however, disallows it. The implication here is that the entity instance passed to the org.hibernate.Session delete method can be either in managed or detached state, while the entity instance passed to remove on javax.persistence.EntityManager must be in managed state.

关于为什么在您找到它之后对象立即分离,我们在没有看到代码的情况下也不知道。但是您可以在上面的同一篇文章中阅读有关实体如何分离以及如何在 3.8 节中重新附加它的内容。使用分离数据

总结一下您应该不应该做什么来防止对象脱离:

  1. 关闭持久性上下文
  2. 清除持久性上下文
  3. 从持久性上下文中驱逐特定实体
  4. 对象序列化(反序列化对象将被分离,序列化对象并不总是)

但如果您无法阻止,您应该使用 Session.saveOrUpdate()、Session.update() 或 Session.merge() 之类的方法重新附加对象。或者.. 简单地使用 hibernate 的(不是 JPA)Session.delete()。

关于java - 删除实体时 hibernate 分离实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com