In my project: Spring Web MVC with OpenSessionInViewFilter,
I have the following code in my obect (Event):
Code:
@JoinTable(
name = "subscription_histories",
joinColumns = @JoinColumn(name = "event_id"),
inverseJoinColumns = @JoinColumn(name = "subscription_history_id")
)
@OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch=FetchType.LAZY)
//@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@LazyCollection(value = LazyCollectionOption.TRUE)
public Set<SubscriptionHistory> getSubscriptionHistories() {
return this.subscriptionHistories;
}
The relation is unidectional
If I save new Event with:
Code:
Event event = (Event) object;
Session session = getSessionFactory().getCurrentSession();
try {
Transaction transaction = session.beginTransaction();
if (event.getId() > 0) {
session.merge(event);
} else {
session.saveOrUpdate(event);
}
transaction.commit();
}catch(.........
I get:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: .....SubscriptionHistory
If I remove the comments at @Cascade I get another Exception:
Code:
org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
What am I doing wrong?
NB: Saving a new Event with no SubscriptionHistory references works okay, and adding these to an existing Event also works fine