title: Hibernate List Mapping Annotation
body: <p>Well i have this problem</p>
  
  <p>These are my tables <img src="https://i.stack.imgur.com/CpHXZ.png" alt="enter image description here"></p>
  
  <p>this is my code for "Compra"</p>
  
  <pre><code>@Entity
  @Table(name = "compra")
  public class Compra implements Serializable {
  
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Column(name = "codigo", unique = true, nullable = false)
       private int codigo;
  
       @ManyToOne
       @JoinColumn(name = "codProveedor", nullable = false)
       private Proveedor proveedor;
  
       @Column(name = "tipoComprobante", nullable = false)
       private String tipoComprobante;
  
       @Temporal(TemporalType.DATE)
       @Column(name = "fechaFactura", nullable = false)
       private Date fechaFactura;
  
       @Temporal(TemporalType.DATE)
       @Column(name = "fechaLlegada", nullable = false)
       private Date fechaLlegada;
  
       @Column(name = "serie", nullable = false)
       private String serie;
  
       @Column(name = "numero", nullable = false)
       private int numero;
  
       @Column(name = "importe", nullable = false)
       private double importe;
  
       @Column(name = "vigencia", nullable = false)
       private boolean vigencia = true;
  
       @ElementCollection    
       private List&lt;DetalleCompra&gt; lstDetalle = new ArrayList&lt;DetalleCompra&gt;();
      // getters and setters ...
  </code></pre>
  
  <p>And this is my code for "DetalleCompra"</p>
  
  <pre><code>@Entity
  @Table(name = "detalleCompra")
  public class DetalleCompra implements Serializable {
  
      @Id
      @GeneratedValue(generator = "gen")
      @GenericGenerator(name = "gen", strategy = "foreign", parameters =       @Parameter(name  = "property", value = "compra"))
      @Column(name = "codCompra", nullable = false)      
      private int codCompra;
  
      @ManyToOne
      @JoinColumn(name = "codPresentacion", nullable = false)
      private Presentacion presentacion;
  
      @Column(name = "imei", nullable = false)
      private String imei;
  
      @Column(name = "simcard", nullable = false)
      private String simcard;
      getters and setters ...
  </code></pre>
  
  <p>Well everything looks fine, but when i want to save i have this problem</p>
  
  <p><strong><em>org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing: DetalleCompra</em></strong></p>
  
  <p>well it is clear because when i want to save Compra and DetalleCompra, the second table expect the fk value</p>
  
  <pre><code>  public void registrar(Compra compra) {
      try {
          session = HibernateUtil.getSessionFactory().openSession();
          trans = session.beginTransaction();                      
          session.save(compra);
          trans.commit();
      } catch (Exception e) {
          trans.rollback();
          throw e;
      } finally {
          session.close();
      }
  }
  </code></pre>
  
  <p>Well the pk of table "compra" is generated well but for the other table does not recognized this value autogenerated, why?, how can i solve that?</p>