休眠删除对象将被重新保存通过级联

问题描述:

我试图删除休眠子实体,但得到这个异常:休眠删除对象将被重新保存通过级联

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",[email protected](name="property", value="user")) 
    private String id; 

@OneToMany(级联= CascadeType.ALL,的mappedBy =“C ustomerId“,orphanRemoval = true) private set 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,没有得到客户该关联例外。谢谢

只需从您的代码中删除session.delete(quote)

@OneToMany收藏是主您的记录,并且它已经被设置为删除孤儿元素。所以,当你做customer.getQuoteConfigs().remove(quote)时,它已经告诉Hibernate从数据库中删除记录。


在一个侧面说明,你应该尝试,而不是使用HQL来获取单个实例session.get(class, id)

您不需要save(customer)因为Hibernate始终跟踪持久实例。交易结束后,它们将自动保存(除非您的会话设置为手动,它不应该)。

+0

感谢Guillaume F.的回复,但仍然给我同样的例外,你知道任何其他方式,我可以删除这个孩子吗? – User1v0

+0

确保所有@OneToMany和@ManyToOne关系都设置为'(fetch = FetchType.LAZY)'加载。另外,请验证您的会话中没有加载其他集合。 –

+0

看看这篇文章,它很有趣:http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-different-types-of-relationships-in-hibernate/24257450 #24257450 –