I'm trying to delete a child entity in hibernate but get this exception:
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)...
these are my entities:
@Entity
@Table(name="CUSTOMER") public class Customer {
@Id
@Column(name="customer_id")
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign",parameters=@Parameter(name="property", value="user"))
private String id;
@OneToMany(cascade=CascadeType.ALL,mappedBy="customerId",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;
}
This the part when I'm trying to remove
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);
My question is how can I remove the child entity QuoteConfiguration, without getting that association exception in Customer. Thanks