Hi
I have two classes which have a Unidirectional One to Many relation with each other.
public class Offer{
...
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name = "Offer_Fields",
joinColumns =
@JoinColumn(name = "OFFER_ID"),
inverseJoinColumns =
@JoinColumn(name = "FIELDMAPPER_ID"))
private Set<FieldMapper> fields = new HashSet<FieldMapper>();
}
@Entity
@Table(name = "FieldMapper")
public class FieldMapper implements Serializable {
@Id
@Column(name = "FIELDMAPPER_ID")
@GeneratedValue
private int id;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "multilingual_field_fk")
private MultiLingual field;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "multilingual_value_fk")
private MultiLingual value;
}
I want to store an Offer with a set of FieldMapper to database. When I Use CascadeType.ALL in my OneToMany, I got this error:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
and when I change CascadeType to something else I got this error:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.RCSTT.library.FieldMapper
and here is where I save Offer :
public void insert(Offer offer) throws SQLException {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(offer);
tx.commit();
session.close();
}
and I don't use session in somewhere else.
in tx.commit();
line throws explained exceptions.
Thanks for your help.