title: #Hibernate is it necessary to save each object that is part of composite-key before saving one that contains it?
body: <p>I have 3 entities. Lest Call it A,B,C and the 4th one that has composite key that is combination of IDs.   </p>
  
  <pre><code>@Entity
  public class A {
      @Id
      @GeneratedValue(generator = "generator")
      private String id;
  }
  
  @Entity
  public class B {
      @Id
      @GeneratedValue(generator = "generator")
      private String id;
  }
  
  
  @Entity
  public class C {
      @Id
      @GeneratedValue(generator = "generator")
      private String id;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "c", fetch = FetchType.LAZY)
      private List&lt;ClassWithCompositeKey&gt; relations = new ArrayList&lt;ClassWithCompositeKey&gt;();
  
  }
  
  @Entity
  public class ClassWithCompositeKey {
  
      @EmbeddedId
      protected CompositeKey compositeKey;
  
      @JoinColumn(name = "A_ID", insertable = false, updatable = false)
      @ManyToOne(optional = false)
      private A a;
  
      @JoinColumn(name = "B_ID", insertable = false, updatable = false)
      @ManyToOne(optional = false)
      private B b;
  
      @JoinColumn(name = "C_ID", insertable = false, updatable = false)
      @ManyToOne(optional = false)
      private C c;
  
      public ClassWithCompositeKey(A a, B b, C c) {
         this.a = a;
         this.b = b;
         this.c = c;
         this.compositeKey = new CompositeKey(a.getId(),b.getId(),c.getId());
      }
  
  }
  
  @Embeddable
  public class CompositeKey {
  
      @Basic(optional = false)
      @Column(name = "A_ID", columnDefinition = "raw")
      private String aId;
      @Basic(optional = false)
      @Column(name = "B_ID", columnDefinition = "raw")
      private String bId;
      @Basic(optional = false)
      @Column(name = "C_ID", columnDefinition = "raw")
      private String cId;
  
      public CompositeKey(String aId, String bId, String cId) {
          this.aId = aId;
          this.bId = bId;
          this.cId = cId;
      }
  }
  </code></pre>
  
  <p>Then when I'm trying to save:</p>
  
  <pre><code>A a = new A();
  B b = new B();
  C c = new C();
  entityManager.persist(a);
  entityManager.persist(b);
  //I'm not saving C
  ClassWithCompositeKey classWithCompositeKey = new ClassWithCompositeKey(a, b, c);
  c.getRelations().add(classWithCompositeKey);
  entityManager.persist(c);
  </code></pre>
  
  <p>I'm getting exception 
  <code>"ConstraintViolationException: Column 'C_ID' cannot be null"</code></p>
  
  <p>It's because c.id is null before "c" will be saved but this value goes to the CompositeKey instance. </p>
  
  <p>I would like to save "c" and have saved collection of ClassWithCompositeKey automatically. However now I first have to save "c" after that attach ClassWithCompositeKey instance to it and save it.
  It is possible (maybe by using other kind of mapping) to have C and ClassWithCompositeKey saved by cascade in one "persist" call ?</p>