title: Using oneToMany relation, but saving data in individual tables at different point of time
body: <p>I am working on a Spring-MVC application which has 2 tables in database and 2 domain classes. Class Person has oneTOMany relation with class Notes. I would like to add Person and notes both in database. So I googled, to find out many MVC based examples for the same problem. However they seem to assume a few things :</p>
  
  <ol>
  <li>Data is being added in a static manner by the developer, mostly through Static void main() or another class.</li>
  <li>Data regarding all the classes which are related is added altogether, eg : Table A has oneToMany relation, so the code will add data for both the tables in one class or one jsp file.</li>
  <li>Other frameworks like Spring-Security at play(This point is understood).</li>
  </ol>
  
  <p>So basically, similar examples with different names and developers is what I found. My problem is :</p>
  
  <ol>
  <li>I don't have static void main, don't intend to use it.</li>
  <li>I am adding data through HTML page wrapped inside JSP page.</li>
  <li>I or the user will first register through the register form, just login later and then add notes, so I am not adding data for both tables at same time. (I have to believe this is possible by Hibernate)</li>
  </ol>
  
  <p>Error : </p>
  
  <pre><code> org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.journaldev.spring.model.Person
          org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)
          org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
          org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:311)
          org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:321)
          org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)
  </code></pre>
  
  <p>Person Model :</p>
  
  <pre><code>@Entity
  @Table(name="person")
  public class Person implements UserDetails{
  
      private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "person_seq_gen")
      @SequenceGenerator(name = "person_seq_gen",sequenceName = "person_seq")
      private int id;
   @OneToMany(cascade = CascadeType.ALL,mappedBy = "person1")
    private Set&lt;Notes&gt; notes1;
  
      public Set&lt;Notes&gt; getNotes1() {
          return notes1;
      }
  
      public void setNotes1(Set&lt;Notes&gt; notes1) {
          this.notes1 = notes1;
      }
  </code></pre>
  
  <p>Notes model :</p>
  
  <pre><code>@Entity
  @Table(name="note")
  public class Notes {
  
      @Id
      @Column(name="noteid")
      @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "note_gen")
      @SequenceGenerator(name = "note_gen",sequenceName = "note_seq")
      private int noteId;
  
   @ManyToOne
     @JoinColumn(name = "id")
     private Person person1;
  
      public Person getPerson1() {
          return person1;
      }
  
      public void setPerson1(Person person1) {
          this.person1 = person1;
      }
  </code></pre>
  
  <p>NotesDAOImpl :</p>
  
  <pre><code>@Transactional
  @Repository
  public class NotesDAOImpl implements NotesDAO{
  
  
  
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sf){
          this.sessionFactory = sf;
      }
  
  
      @Override
      public void addNote(Notes notes, int id) {
         Session session = this.sessionFactory.getCurrentSession();
         session.save(notes);
      }
  </code></pre>
  
  <p>SQL schema :</p>
  
  <pre><code>CREATE TABLE public.person (
                  id INTEGER NOT NULL,
                  firstname VARCHAR,
                  username VARCHAR,
                  password VARCHAR,
                  CONSTRAINT personid PRIMARY KEY (id)
  );
  
  
  CREATE TABLE public.note (
                  noteid INTEGER NOT NULL,
                  sectionid INTEGER,
                  canvasid INTEGER,
                  text VARCHAR,
                  notecolor VARCHAR,
                  noteheadline VARCHAR,
                  id INTEGER NOT NULL,
                  CONSTRAINT noteid PRIMARY KEY (noteid)
  );
  
  
  ALTER TABLE public.note ADD CONSTRAINT user_note_fk
  FOREIGN KEY (id)
  REFERENCES public.person (id)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
  NOT DEFERRABLE;
  </code></pre>
  
  <p>Btw, the id in addNote method is just me checking if SpringSecurity is actually sending userid, and has properly loggedin, debug purpose.</p>
  
  <p>So, I am unable to add notes once user is logged in, what am I doing wrong? Or this is not possible with Hibernate. In that case, let me find a gun to shoot myself.. :P </p>