Problem with Hibernate

2

I just had a problem when inserting a product (in this case) in the database. I'm working with the mapping and it always returns "false". I can not find where the possible failure may be.

I show the classes.

Class Mapping Products:

public class Productos  implements java.io.Serializable {


     private String codigoProducto;
     private Categoriasproductos categoriasproductos;
     private String nombreProducto;
     private String escala;
     private String vendedor;
     private String descripcion;
     private short unidadesStock;
     private BigDecimal precioCompra;
     private BigDecimal precioVenta;

    @Override
    public String toString() {
        return "Productos{" + "codigoProducto=" + codigoProducto + ", categoriasproductos=" + categoriasproductos + ", nombreProducto=" + nombreProducto + ", escala=" + escala + ", vendedor=" + vendedor + ", descripcion=" + descripcion + ", unidadesStock=" + unidadesStock + ", precioCompra=" + precioCompra + ", precioVenta=" + precioVenta + '}';
    }




    public Productos() {
    }

    public Productos(String codigoProducto, Categoriasproductos categoriasproductos, String nombreProducto, String escala, String vendedor, String descripcion, short unidadesStock, BigDecimal precioCompra, BigDecimal precioVenta) {
       this.codigoProducto = codigoProducto;
       this.categoriasproductos = categoriasproductos;
       this.nombreProducto = nombreProducto;
       this.escala = escala;
       this.vendedor = vendedor;
       this.descripcion = descripcion;
       this.unidadesStock = unidadesStock;
       this.precioCompra = precioCompra;
       this.precioVenta = precioVenta;
    }

    public String getCodigoProducto() {
        return this.codigoProducto;
    }

    public void setCodigoProducto(String codigoProducto) {
        this.codigoProducto = codigoProducto;
    }
    public Categoriasproductos getCategoriasproductos() {
        return this.categoriasproductos;
    }

    public void setCategoriasproductos(Categoriasproductos categoriasproductos) {
        this.categoriasproductos = categoriasproductos;
    }
    public String getNombreProducto() {
        return this.nombreProducto;
    }

    public void setNombreProducto(String nombreProducto) {
        this.nombreProducto = nombreProducto;
    }
    public String getEscala() {
        return this.escala;
    }

    public void setEscala(String escala) {
        this.escala = escala;
    }
    public String getVendedor() {
        return this.vendedor;
    }

    public void setVendedor(String vendedor) {
        this.vendedor = vendedor;
    }
    public String getDescripcion() {
        return this.descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    public short getUnidadesStock() {
        return this.unidadesStock;
    }

    public void setUnidadesStock(short unidadesStock) {
        this.unidadesStock = unidadesStock;
    }
    public BigDecimal getPrecioCompra() {
        return this.precioCompra;
    }

    public void setPrecioCompra(BigDecimal precioCompra) {
        this.precioCompra = precioCompra;
    }
    public BigDecimal getPrecioVenta() {
        return this.precioVenta;
    }

    public void setPrecioVenta(BigDecimal precioVenta) {
        this.precioVenta = precioVenta;
    }


}

Class Operations with the insert method:

public class Operaciones {

Session sesion = null;

    public Operaciones() {
        sesion = HibernateConnector.getInstance().getSession();
    }


 public ArrayList<Productos>getProductos() { 

        ArrayList<Productos>array = new ArrayList<>(); 

        array = (ArrayList<Productos>) sesion.createQuery("from Productos").list();

        return array;


public boolean addProductos(Productos producto) { 

Transaction transaccion = null;

 try {

transaccion = sesion.beginTransaction();

            sesion.save(producto);

            transaccion.commit();

            return true;
            } catch(HibernateException e) {
                transaccion.rollback();
                return false;
            }
         }
       } 



    }

Finally the main:

public static void main(String[] args) {

        Operaciones op = new Operaciones();

        Productos p1 = new Productos("1111111",  null, "Producto47", "1:10", "Carlos Antonio", "la novedad de nuestra tienda", new Short("2"), null, null);

        System.out.println(op.addProductos(p1));


        ArrayList<Productos>array = op.getProductos();

        for (Productos productos : array) {

            System.out.println(productos.toString());

        }
}

}

Always return false :

Exceptions:

    
asked by guille3 04.09.2018 в 10:55
source

1 answer

1

Capturing an exception and not treating it correctly is a classic rookie failure that we all go through:

try {
      transaccion = sesion.beginTransaction();
      sesion.save(producto);
      transaccion.commit();
      return true;
} catch(HibernateException e) {
      // ¿Qué error ha ocurrido?
      e.printStackTrace();
      transaccion.rollback();
      return false;
}

The line I added will cause the error message to appear on the console of your IDE. It can be something as silly as there is already a product with the same identifier (I do not know what your primary key is for the product table).

By the way, the table can be called either producto or productos (singular or plural), but since each instance of the class represents a single product, the correct name should be Producto and not Productos . An object Productos can imply that it is a collection , not a single element

    
answered by 04.09.2018 / 11:11
source