一对多关系中的 CascadeType 问题

发布于 2024-09-02 23:03:06 字数 1554 浏览 14 评论 0原文

我有两个类,彼此之间具有单向一对多关系。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-09-09 23:03:06

第一个例外让我相信这不是映射/实体的问题,而是对象生命周期或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文