一对多关系中的 CascadeType 问题
我有两个类,彼此之间具有单向一对多关系。
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
这是我保存 Offer 的位置:
public void insert(Offer offer) throws SQLException {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(offer);
tx.commit();
session.close();
}
并且我不在其他地方使用会话。
在 tx.commit(); 行中抛出已解释的异常。
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个例外让我相信这不是映射/实体的问题,而是对象生命周期或 Hibernate 会话处理方式的问题。
您尝试保存的 FieldMappers 是否已经持久存在(在另一个会话中)?您可能需要先将它们分离。
The first exception leads me to believe that this is not a problem with your mapping/entities but with how the object lifecycle or Hibernate session is handled.
Are the FieldMappers that you're trying to save already persistent (in another session)? You might need to detach those first.