我有两个彼此具有单向一对多关系的类。
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;
}
我想将带有一组 FieldMapper 的 Offer 存储到数据库中。当我在 OneToMany 中使用 CascadeType.ALL 时,出现此错误:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
当我将 CascadeType 更改为其他内容时,出现此错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.RCSTT.library.FieldMapper
这是我保存优惠的地方:
public void insert(Offer offer) throws SQLException {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(offer);
tx.commit();
session.close();
}
而且我不在其他地方使用会话。
在线抛出解释的tx.commit();
异常。
谢谢你的帮助。