title: Hibernate @ManyToOne mapping: object references an unsaved transient instance
body: <p>I have two classes like these:</p>
  
  <pre><code>public class Parent {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "DBId")
      private Long DBId = null;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @LazyToOne(LazyToOneOption.FALSE)
      private SampleSet samples;
  }
  
  public class SampleSet {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "DBId")
      private Long DBId = null;
  
      private String uniqueId;
  
      @ElementCollection(fetch = FetchType.EAGER)
      @CollectionTable(name = "SampleSet_samplesSet")
      private Set&lt;String&gt; samplesSet = new TreeSet&lt;&gt;();
  }
  </code></pre>
  
  <p>and I have a code like this:</p>
  
  <pre><code>public void modifyAndSaveParent(Parent p, Session session) {
      SampleSet s = (SampleSet) session.getNamedQuery("SampleSet.loadByUniqueId").setParameter("uniqueId", p.getSampleSet().getUniqueId()).uniqueResult();
      if (s == null)
          throw new RuntimeException();
      else
          p.setSampleSet(s);
  
      session.save(p);
      session.flush();
  }
  </code></pre>
  
  <p>The problem is that an exception is thrown on session.flush():</p>
  
  <pre><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: SampleSet
          at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:249)
          at org.hibernate.type.EntityType.getIdentifier(EntityType.java:509)
          at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:309)
          at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:319)
          at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)
          at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4051)
          at org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:535)
          at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:235)
          at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:164)
          at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:228)
          at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:100)
          at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
          at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240)
  </code></pre>
  
  <p>But as you can see the SampleSet must be already persisted. What should I do?</p>
  
  <p>In fact, I don't want to use cascades. I want to manage SampleSets manually and don't want the changes to Parent to be cascaded.</p>