title: Hibernate JPA IdentifierGenerationException: null id generated for class with @embeddedid
body: <p>I am having trouble mapping my database domain model to the program entities in one case where the entity is essentially a join table (a period) which combines two other entities (a timeslot and a day). Another entity (a lesson) then has a reference to this period entity, determining when it occurs.</p>
  
  <p>When I try to save a lesson with a new period using <code>saveOrUpdate(lesson)</code> hibernate throws an IdentifierGenerationException</p>
  
  <blockquote>
    <p>org.hibernate.id.IdentifierGenerationException: null id generated for:class com.trials.domain.Period</p>
  </blockquote>
  
  <p>The database looks like below (not the real database, just the key tables and columns)</p>
  
  <p><img src="https://i.stack.imgur.com/HWXsz.png" alt="enter image description here"></p>
  
  <p>In the java hibernate model, I have used an embedded id for the primary key of the period class and the lesson class then has a reference to a period.</p>
  
  <p><strong>Period.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "period")
  public class Period{
      @EmbeddedId
      private PeriodId periodId;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "day_idday", nullable = false, insertable = false, updatable = false)
      private Day day;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "timeslot_idtimeslot", nullable = false, insertable = false, updatable = false)
      private Timeslot timeslot;
  
      //constructors, getters, setters, hashcode, and equals
  }
  </code></pre>
  
  <p>And the embedded id just has the primary key columns:</p>
  
  <p><strong>PeriodId.java</strong></p>
  
  <pre><code>@Embeddable
  public class PeriodId implements Serializable {
      @Column(name = "timeslot_idtimeslot")
      private int timeslotId;
  
      @Column(name = "day_idday")
      private int dayId;
  
      //constructors, getters, setters, hashcode, and equals
  }
  </code></pre>
  
  <p>Then there is the lesson class that uses the period defined as:</p>
  
  <p><strong>Lesson.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "lesson")
  public class Lesson {
      @Id
      @Column(name = "idlesson")
      private int lessonId;
  
      @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinColumns({@JoinColumn(name = "period_timeslot_idtimeslot", nullable = false, updatable = false), @JoinColumn(name = "period_day_idday", nullable = false, updatable = false)})
      private Period period;
      //constructors, getters, setters, hashcode, and equals
  }
  </code></pre>
  
  <p>The Timeslot and Day entity classes are both very basic pojos, and their ids use <code>GenerationType.AUTO</code>. So my problems are:</p>
  
  <ol>
  <li>What causes this IdentifierGenerationException</li>
  <li>How to avoid it while keeping the same database model</li>
  </ol>
  
  <p>Thanks in advance</p>