1

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.

Milad.KH
  • 11
  • 4
  • Just out of curiosity, are the One to Many relationships involving the same set of columns in the tables? I'm just wondering how this mathematically possible. – Vineet Reynolds May 24 '10 at 20:24
  • No. Each class has its own columns in its own table but In my case a Join table will be created to manage the relation (you can create a One to Many relation without a join table). – Milad.KH May 24 '10 at 20:43
  • Well, it might be useful to post the code of the FieldMapper class then. For some reason, I believe this is a ManyToMany relationship. – Vineet Reynolds May 25 '10 at 12:54

1 Answers1

0

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.

Magnus Eklund
  • 649
  • 6
  • 9
  • No, they just created in java code when I want to save Offer. They are not persistent, so when I change cascadeType to something else, I got this : "object references an unsaved transient instance". it means they are not persistent. – Milad.KH May 25 '10 at 04:49
  • How is your hibernate session handled? This exception can occur if the collection (fields) is associated with two open sessions at the same time. So, if you for example lookup an Offer using one session and then try to save the same Offer using a different session this exception will occur. – Magnus Eklund May 25 '10 at 10:57
  • No, Offer is also created in code. What do you mean about How is hibernate session handled ? – Milad.KH May 25 '10 at 12:39
  • I'm afraid I have no more ideas :( – Magnus Eklund May 26 '10 at 09:43