Foros del Web » Programación para mayores de 30 ;) » Java »

Hibernate - Uno a muchos (Proceso Venta)

Estas en el tema de Hibernate - Uno a muchos (Proceso Venta) en el foro de Java en Foros del Web. Saludos a todo nuevamente, estoy trabajando con hibernate, ya tengo todos los crud, ahora llevo horas con el proceso de venta, tengo estas tablas. Producto ...
  #1 (permalink)  
Antiguo 21/01/2015, 22:14
 
Fecha de Ingreso: diciembre-2014
Ubicación: Lima
Mensajes: 68
Antigüedad: 9 años, 4 meses
Puntos: 0
Hibernate - Uno a muchos (Proceso Venta)

Saludos a todo nuevamente, estoy trabajando con hibernate, ya tengo todos los crud, ahora llevo horas con el proceso de venta, tengo estas tablas.
Producto
Cliente
Empleado
Factura
DetalleFactura

Esta es mi clase Venta
Código Java:
Ver original
  1. package modelo;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.FetchType;
  8. import javax.persistence.Id;
  9. import javax.persistence.JoinColumn;
  10. import javax.persistence.ManyToOne;
  11. import javax.persistence.OneToMany;
  12. import javax.persistence.Table;
  13.  
  14. @Table(name = "venta", catalog = "bdventas"
  15. )
  16. public class Venta implements java.io.Serializable {
  17.  
  18.     private int venId;
  19.     private Empleado empleado;
  20.     private Cliente cliente;
  21.     private double venIgv;
  22.     private double venTotal;
  23.     private double venMontototal;
  24.     private String venIndicador;
  25.     private Set<DetalleVenta> detalleVentas = new HashSet(0);
  26.  
  27.     public Venta() {
  28.         this.venId = 0;
  29.         this.cliente = new Cliente();
  30.         this.empleado = new Empleado();
  31.     }
  32.  
  33.     public Venta(int venId) {
  34.         this.venId = venId;
  35.     }
  36.  
  37.     public Venta(int venId, Empleado empleado, Cliente cliente, double venIgv, double venTotal, double venMontototal, String venIndicador, Set<DetalleVenta> detalleVentas) {
  38.         this.venId = venId;
  39.         this.empleado = empleado;
  40.         this.cliente = cliente;
  41.         this.venIgv = venIgv;
  42.         this.venTotal = venTotal;
  43.         this.venMontototal = venMontototal;
  44.         this.venIndicador = venIndicador;
  45.         this.detalleVentas = detalleVentas;
  46.     }
  47.  
  48.     public Venta(int venId, Empleado empleado, Cliente cliente, double venIgv, double venTotal, double venMontototal, String venIndicador) {
  49.         this.venId = venId;
  50.         this.empleado = empleado;
  51.         this.cliente = cliente;
  52.         this.venIgv = venIgv;
  53.         this.venTotal = venTotal;
  54.         this.venMontototal = venMontototal;
  55.         this.venIndicador = venIndicador;
  56.     }
  57.  
  58.     @Id
  59.  
  60.     @Column(name = "ven_id", unique = true, nullable = false)
  61.     public int getVenId() {
  62.         return this.venId;
  63.     }
  64.  
  65.     public void setVenId(int venId) {
  66.         this.venId = venId;
  67.     }
  68.  
  69.     @ManyToOne(fetch = FetchType.LAZY)
  70.     @JoinColumn(name = "ven_empleado")
  71.     public Empleado getEmpleado() {
  72.         return this.empleado;
  73.     }
  74.  
  75.     public void setEmpleado(Empleado empleado) {
  76.         this.empleado = empleado;
  77.     }
  78.  
  79.     @ManyToOne(fetch = FetchType.LAZY)
  80.     @JoinColumn(name = "ven_cliente")
  81.     public Cliente getCliente() {
  82.         return this.cliente;
  83.     }
  84.  
  85.     public void setCliente(Cliente cliente) {
  86.         this.cliente = cliente;
  87.     }
  88.  
  89.     @Column(name = "ven_IGV", precision = 8)
  90.     public double getVenIgv() {
  91.         return this.venIgv;
  92.     }
  93.  
  94.     public void setVenIgv(double venIgv) {
  95.         this.venIgv = venIgv;
  96.     }
  97.  
  98.     @Column(name = "ven_total", precision = 8)
  99.     public double getVenTotal() {
  100.         return this.venTotal;
  101.     }
  102.  
  103.     public void setVenTotal(double venTotal) {
  104.         this.venTotal = venTotal;
  105.     }
  106.  
  107.     @Column(name = "ven_montototal", precision = 8)
  108.     public double getVenMontototal() {
  109.         return this.venMontototal;
  110.     }
  111.  
  112.     public void setVenMontototal(double venMontototal) {
  113.         this.venMontototal = venMontototal;
  114.     }
  115.  
  116.     @Column(name = "ven_indicador", length = 2)
  117.     public String getVenIndicador() {
  118.         return this.venIndicador;
  119.     }
  120.  
  121.     public void setVenIndicador(String venIndicador) {
  122.         this.venIndicador = venIndicador;
  123.     }
  124.  
  125.     @OneToMany(fetch = FetchType.LAZY, mappedBy = "venta")
  126.     public Set<DetalleVenta> getDetalleVentas() {
  127.         return this.detalleVentas;
  128.     }
  129.  
  130.     public void setDetalleVentas(Set<DetalleVenta> detalleVentas) {
  131.         this.detalleVentas = detalleVentas;
  132.     }
  133.  
  134. }
Esta es mi clase DetalleVenta
Código Java:
Ver original
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4.  
  5. <hibernate-mapping>
  6.     <class name="modelo.Venta" table="venta" catalog="bdventas">
  7.         <id name="venId" type="int">
  8.             <column name="ven_id" />
  9.             <generator class="assigned" />
  10.         </id>
  11.         <many-to-one name="empleado" class="modelo.Empleado" fetch="select">
  12.             <column name="ven_empleado" />
  13.         </many-to-one>
  14.         <many-to-one name="cliente" class="modelo.Cliente" fetch="select">
  15.             <column name="ven_cliente" />
  16.         </many-to-one>
  17.         <property name="venIgv" type="double">
  18.             <column name="ven_IGV" precision="8" />
  19.         </property>
  20.         <property name="venTotal" type="double">
  21.             <column name="ven_total" precision="8" />
  22.         </property>
  23.         <property name="venMontototal" type="double">
  24.             <column name="ven_montototal" precision="8" />
  25.         </property>
  26.         <property name="venIndicador" type="string">
  27.             <column name="ven_indicador" length="2" />
  28.         </property>
  29.         <set name="detalleVentas" table="detalle_venta" inverse="true" lazy="true" fetch="select">
  30.             <key>
  31.                 <column name="dv_venta" not-null="true" />
  32.             </key>
  33.             <one-to-many class="modelo.DetalleVenta" />
  34.         </set>
  35.     </class>
  36. </hibernate-mapping>

Nota: Las clases y los archivos hbm.xml, los genera el mismo netbeans, pues cuando lo hago manualmente, el archivo .cfg de hibernate no me reconoce.

Y este es un test que hice para guardar el proceso
Código Java:
Ver original
  1. @Test
  2.     public void addVenta() {
  3.         Session session = HibernateUtil.getSessionFactory().openSession();
  4.         try {
  5.             session.getTransaction().begin();
  6.             Venta venta = new Venta();
  7.             Set<DetalleVenta> detalles = new HashSet<DetalleVenta>(0);
  8.             venta.setVenId(1);
  9.             venta.setCliente(new Cliente(1));
  10.             venta.setEmpleado(new Empleado(1));
  11.             venta.setVenIgv(18.0);
  12.             venta.setVenTotal(129.0);
  13.             venta.setVenMontototal(523.3);
  14.             venta.setVenIndicador("S");
  15.             detalles.add(new DetalleVenta(1, new Venta(1), new Producto(3), 5, 47.6, 87.2));
  16.             detalles.add(new DetalleVenta(2, new Venta(1), new Producto(1), 6, 44.6, 35.2));
  17.             detalles.add(new DetalleVenta(3, new Venta(1), new Producto(2), 7, 42.6, 326.2));
  18.             venta.setDetalleVentas(detalles);
  19.             session.save(venta);
  20.             session.getTransaction().commit();
  21.             System.out.println("Guardado");
  22.         } catch (Exception e) {
  23.             session.getTransaction().rollback();
  24.             System.out.println("No se pudo guardar");
  25.         }
  26.         session.close();
  27.     }

Y este es la clase que uso para guardar, es decir estuve viendo otra forma de guardar y nada.
Código Java:
Ver original
  1. public void createVenta() {
  2.         try {
  3.             VentaDao ventaDao = new VentaDaoImpl();
  4.             String msg;
  5.             this.venta.setVenId(Integer.parseInt(dlgVentas.txtCodVenta.getText()));
  6.             this.venta.setCliente(cliente);
  7.             this.venta.setEmpleado(new Empleado(Integer.parseInt(dlgVentas.lblEmpID.getText())));
  8.             this.venta.setVenIgv(Double.parseDouble(txtIGV.getText()));
  9.             this.venta.setVenTotal(Double.parseDouble(txtSubTotal.getText()));
  10.             this.venta.setVenMontototal(Double.parseDouble(txtTotal.getText()));
  11.             this.venta.setVenIndicador("S");
  12.  
  13.             int fila = dlgVentas.jTable1.getRowCount();
  14.             for (int i = 0; i < dlgVentas.jTable1.getRowCount(); i++) {
  15.                 this.dventa.setDvId(i + 1);
  16.                 this.dventa.setProducto(new Producto(Integer.parseInt(dlgVentas.jTable1.getValueAt(i, 1).toString())));
  17.                 this.dventa.setDvCantidad(Integer.parseInt(dlgVentas.jTable1.getValueAt(i, 0).toString()));
  18.                 this.dventa.setVdPrecio(Double.parseDouble(dlgVentas.jTable1.getValueAt(i, 3).toString()));
  19.                 this.dventa.setDvTotal(Double.parseDouble(dlgVentas.jTable1.getValueAt(i, 4).toString()));
  20.                 this.dventa.setVenta(this.venta);
  21.                 this.venta.getDetalleVentas().add(this.dventa);
  22.             }
  23.             if (ventaDao.create(this.venta)) {
  24.                 msg = "Venta Registrada";
  25.                 Mensajes.informacion(msg);
  26.             } else {
  27.                 msg = "Error al registrar la venta";
  28.                 Mensajes.error(msg);
  29.             }
  30.         } catch (Exception e) {
  31.             System.out.println("No se guardo " + e.getMessage());
  32.         }
  33.     }

En ambos casos solo me guarda la venta y no el detalle, espero alguien me pueda ayudar a dar solucion.
Gracias de antemano
  #2 (permalink)  
Antiguo 08/02/2015, 21:09
 
Fecha de Ingreso: febrero-2015
Mensajes: 14
Antigüedad: 9 años, 2 meses
Puntos: 1
Respuesta: Hibernate - Uno a muchos (Proceso Venta)

Proba con un cascade="all" en el set de detalleVentas.

Por otro lado; La entidad DetalleVenta tiene la venta asociada en el constructor? Para haces que eso?

Etiquetas: clase, hibernate, jtable, muchos, proceso, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:34.