为了解释我的问题,我创建了这个简单的项目,包含两个表: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", catalog = "test", 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 + "]";
}
}
提前致谢, Souri
答案 0 :(得分:0)
您应该只能删除订单记录并将其删除所有关联的项记录。为此,您需要为Order和Items之间的关联添加另一个级联类型。
@OneToMany(cascade=CascadeType.ALL, mappedBy = "order1", fetch=FetchType.EAGER, orphanRemoval=true)
private List<Item> itemList;
这告诉hibernate当你删除父命令时,它应该删除子项记录。
session.delete(order);
此删除将级联到子项记录并删除它们。
答案 1 :(得分:0)
@Alex这是ItemJpaController#destroy()
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();
}
}
}
而且,我使用的hibernate版本没有DELETE_ORPHAN
级联类型。
另外,我如何获得会话?我应该在JpaController
方法内的destroy()
类中,还是从我创建/删除对象的邮件方法中获取它。?