Question

I need help with this. With code is more clear, this is my function to persist:

public String finalizarCompra() {
  Pedido pedido = new Pedido();
  pedido.setEstado("almacen");
  pedido.setFechaVenta(new Date());
  pedido.setIdUsuario(loginBean.getUsuario());
  Producto p;
  Integer i;
  DetPedido detPedido;
  List<DetPedido> lista = new ArrayList<>();
  for (Map.Entry e : productos.entrySet()) {
    detPedido = new DetPedido();
    p = (Producto) e.getKey();
    i = (Integer) e.getValue();
    detPedido.setProducto(p);
    detPedido.setCantidad(i);
    detPedido.setPrecioUnidad(p.getPrecioUnidad());
    detPedido.setPedido(pedido);
    lista.add(detPedido);
    detPedidoBean.insert(detPedido);
  }
  pedido.setDetPedidoCollection(lista);
  pedidoBean.insert(pedido);
  return "";
}

This is my Pedido Entity:

@Entity
public class Pedido implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "ID_PEDIDO")
  private Integer idPedido;

  @Basic(optional = false)
  @NotNull
  @Column(name = "FECHA_VENTA")
  @Temporal(TemporalType.TIMESTAMP)
  private Date fechaVenta;

  @Column(name = "FECHA_ENVIO")
  @Temporal(TemporalType.TIMESTAMP)
  private Date fechaEnvio;

  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 50)
  @Column(name = "ESTADO")
  private String estado;

  @JoinColumn(name = "ID_USUARIO", referencedColumnName = "ID_USUARIO")
  @ManyToOne(optional = false)
  private Usuario idUsuario;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "pedido")
  private Collection<DetPedido> detPedidoCollection;

  // Getters and Setters //

This is my DetPedido Entity:

@Entity
public class DetPedido implements Serializable {
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  protected DetPedidoPK detPedidoPK;

  @Basic(optional = false)
  @NotNull
  @Column(name = "CANTIDAD")
  private Integer cantidad;

  @Basic(optional = false)
  @NotNull
  @Column(name = "PRECIO_UNIDAD")
  private Double precioUnidad;

  @JoinColumn(name = "ID_PRODUCTO", referencedColumnName = "ID_PRODUCTO", insertable = false, updatable = false)
  @ManyToOne(optional = false)
  private Producto producto;

  @JoinColumn(name = "ID_PEDIDO", referencedColumnName = "ID_PEDIDO", insertable = false, updatable = false)
  @ManyToOne(optional = false)
  private Pedido pedido;

  // Getters and Setters //

And this is my DetPedidoPK:

@Embeddable
public class DetPedidoPK implements Serializable {

  @Basic(optional = false)
  @NotNull
  @Column(name = "ID_PEDIDO")
  private Integer idPedido;

  @Basic(optional = false)
  @NotNull
  @Column(name = "ID_PRODUCTO")
  private Integer idProducto;

  // Getters and Setters //

The Entities ara generated automatically from the Database, also DetPedidoPK, and now I don't know how to save a Pedido. I tried with the code above, but it doesn't work.

Can anybody help me?

Greetings.

Was it helpful?

Solution

If you are using JPA 1.0 and this entity model, then you will need to persist and flush both Producto and Pedido instances to have their IDs assigned before you can persist the DetPedido instance that will reference them. Once that is done, you will need to manually set the id values in DetPedido's DetPedidoPK instance so that they match the referenced Producto and DetPedido key values. You cannot insert DetPedido without the DetPedidoPK values having been set.

JPA 2.0 supports derived IDs, which allows marking the relationship as either @ID or @MapsId, indicating that the ID values should be pulled from the joincolumn associated to the relationship. In this case, it would become:

  @ManyToOne(optional = false)
  @MapsId("idProducto")
  private Producto producto;


  @ManyToOne(optional = false)
  @MapsId("idPedido")
  private Pedido pedido;

If you wanted, you could do away with the embeddable within DetPedido and just mark the relationships as the @Id, and because it is composite you would use the DetPedidoPK as the PK class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top