问题描述
|
为了说明我的问题,我创建了一个带有两个表的简单项目:item和order1(其中orderId作为外键的项目)。
现在,当我尝试从订单表中删除一行时,出现错误,指出特定的订单ID与\'item \'表中的少量商品相关联。
因此,我要做的是遍历所有项目对象(使用\'Eager \'加载),并为每个对象调用destroy()。删除所有项目后,我调用order.destroy()。
但是现在,我收到以下错误:
Exception in thread \"main\" javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
at com.sourind.test.testorm.controller.ItemJpaController.destroy(ItemJpaController.java:112)
at com.sourind.test.testorm.App.main(App.java:31)
Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [com.sourind.test.testorm.entity.Item#<null>]
我正在使用Hibernate(JPA 2.0)。在这方面的任何帮助将不胜感激。
作为参考,我正在复制netbeans创建的实体类:
@Entity
@Table(name = \"item\",catalog = \"test\",schema = \"\")
@NamedQueries({
@NamedQuery(name = \"Item.findAll\",query = \"SELECT i FROM Item i\"),@NamedQuery(name = \"Item.findById\",query = \"SELECT i FROM Item i WHERE i.id = :id\"),@NamedQuery(name = \"Item.findByName\",query = \"SELECT i FROM Item i WHERE i.name = :name\")})
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = \"id\")
private Integer id;
@Basic(optional = false)
@Column(name = \"name\")
private String name;
@JoinColumn(name = \"orderId\",referencedColumnName = \"id\")
@ManyToOne(optional = false)
private Order1 order1;
public Item() {
}
public Item(Integer id) {
this.id = id;
}
public Item(Integer id,String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Order1 getorder1() {
return order1;
}
public void setorder1(Order1 order1) {
this.order1 = order1;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// Todo: Warning - this method won\'t work in the case the id fields are not set
if (!(object instanceof Item)) {
return false;
}
Item other = (Item) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return \"com.sourind.test.testorm.entity.Item[id=\" + id + \"]\";
}
}
和order1类:
@Entity
@Table(name = \"order\",schema = \"\")
@NamedQueries({
@NamedQuery(name = \"Order1.findAll\",query = \"SELECT o FROM Order1 o\"),@NamedQuery(name = \"Order1.findById\",query = \"SELECT o FROM Order1 o WHERE o.id = :id\"),@NamedQuery(name = \"Order1.findByOrderDate\",query = \"SELECT o FROM Order1 o WHERE o.orderDate = :orderDate\")})
public class Order1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = \"id\")
private Integer id;
@Basic(optional = false)
@Column(name = \"orderDate\")
@Temporal(TemporalType.TIMESTAMP)
private Date orderDate;
@OnetoMany(cascade = CascadeType.ALL,mappedBy = \"order1\",fetch=FetchType.EAGER)
private List<Item> itemList;
public Order1() {
}
public Order1(Integer id) {
this.id = id;
}
public Order1(Integer id,Date orderDate) {
this.id = id;
this.orderDate = orderDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getorderDate() {
return orderDate;
}
public void setorderDate(Date orderDate) {
this.orderDate = orderDate;
}
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// Todo: Warning - this method won\'t work in the case the id fields are not set
if (!(object instanceof Order1)) {
return false;
}
Order1 other = (Order1) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return \"com.sourind.test.testorm.entity.Order1[id=\" + id + \"]\";
}
}
提前致谢,
苏里
解决方法
@Alex这是ѭ3code的代码
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Item item;
try {
item = em.getReference(Item.class,id);
item.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException(\"The item with id \" + id + \" no longer exists.\",enfe);
}
Order1 order1 = item.getOrder1();
if (order1 != null) {
order1.getItemList().remove(item);
order1 = em.merge(order1);
}
em.remove(item);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
而且,我使用的休眠版本没有DELETE_ORPHAN
级联类型。
另外,我如何获得会议?我应该在destroy()
方法中的JpaController
类中还是在我从中创建/删除对象的mail方法中获取它?
,您应该只能够删除订单记录,并删除所有关联的项目记录。为此,您需要向订单和商品之间的关联添加另一种层叠类型。
@OneToMany(cascade=CascadeType.ALL,mappedBy = \"order1\",fetch=FetchType.EAGER,orphanRemoval=true)
private List<Item> itemList;
这告诉休眠状态,当您删除父订单时,它应该删除子项记录。
session.delete(order);
此删除操作将层叠到子Item记录并将其删除。