我正在尝试删除hibernate中的子实体,但是得到了这个异常:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.epic.ecommerce.core.model.Customer#newnameTest]
at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1236)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)...
这些是我的实体:
@Entity
@Table(名称= “客户”) 公共类客户{
@Id
@Column(name="customer_id")
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign",parameters=@Parameter(name="property", value="user"))
private String id;
@OneToMany(级联= CascadeType.ALL,的mappedBy = “客户ID”,orphanRemoval =真) 私人套装quoteConfigs;
public Set<QuoteConfiguration> getQuoteConfigs() {
return quoteConfigs;
}
public void setQuoteConfigs(Set<QuoteConfiguration> quoteConfigs) {
this.quoteConfigs = quoteConfigs;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne
@PrimaryKeyJoinColumn
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Entity
@Table(name="quote_configuration")
public class QuoteConfiguration implements java.io.Serializable{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "quote_id",unique = true, nullable = false)
private Integer quoteId;
@Column(name = "base_product", nullable = false)
private String baseProduct;
@Column(name = "quote_name")
private String quoteName;
/*
@Column(name = "customer_customer_id")
private String customerId;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
*/
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "customer_customer_id")
private Customer customerId;
public Customer getCustomerId() {
return customerId;
}
public void setCustomerId(Customer customerId) {
this.customerId = customerId;
}
这是我试图删除时的部分
Session session = this.sessionFactory.getCurrentSession();
Query q = session.createQuery("from QuoteConfiguration where quoteId = :quoteId ");
q.setParameter("quoteId", quoteId);
QuoteConfiguration quote = (QuoteConfiguration)q.list().get(0);
Query qc=session.createQuery("from Customer where id=:id");
qc.setParameter("id", quote.getCustomerId().getId());
Customer customer=(Customer)qc.list().get(0);
customer.getQuoteConfigs().remove(quote);
session.delete(quote);
session.save(customer);
我的问题是如何删除子实体QuoteConfiguration,而不在Customer中获取该关联异常。感谢
答案 0 :(得分:0)
只需从代码中删除session.delete(quote)
即可。
您的@OneToMany
收藏集是您记录的主,并且已设置为删除孤儿元素。因此,当你执行customer.getQuoteConfigs().remove(quote)
时,它已经告诉Hibernate从数据库中删除记录。
另外,您应该尝试session.get(class, id)
而不是使用HQL来获取单个实例。
您不需要save(customer)
,因为Hibernate始终会跟踪持久化实例。交易结束时,它们将自动保存(除非您的会话设置为手动,不应该是手动)。