abdou amer
abdou amer

Reputation: 849

Hibernate and Postgresql negative primary key id issue

I'm using Hibernate 5.0.7 with Postgresql DBMS. Every thing working just fine, but I face a issue with primary key in postgresql database (Negative value).

Here is my code:

package model;

import javafx.beans.property.*;
import javax.persistence.*;


@Entity
@Table(name = "Product")
@Access(AccessType.PROPERTY)
public class Product {
    private LongProperty idProduct;
    private StringProperty nameFr;
    private DoubleProperty qtyInHand;
    private DoubleProperty sellPrice;


 public Product() {
        idProduct = new SimpleLongProperty();      
        nameFr = new SimpleStringProperty();
        qtyInHand = new SimpleDoubleProperty();
        sellPrice = new SimpleDoubleProperty();

    }

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq_gen")
    @SequenceGenerator(name = "product_seq_gen", sequenceName = "product_idproduct_seq")
    @Column(name = "idproduct", unique = true, nullable = false)
    public Long getIdProduct() {
        return idProduct.get();
    }

    public LongProperty idProductProperty() {
        return idProduct;
    }

    public void setIdProduct(Long idProduct) {
        this.idProduct.set(idProduct);
    }

@Column(name = "nameFr")
    public String getNameFr() {
        return nameFr.get();
    }

    public StringProperty nameFrProperty() {
        return nameFr;
    }

    public void setNameFr(String nameFr) {
        this.nameFr.set(nameFr);
    }

@Column(name = "qtyInHand")
    public double getQtyInHand() {
        return qtyInHand.get();
    }

    public DoubleProperty qtyInHandProperty() {
        return qtyInHand;
    }

    public void setQtyInHand(double qtyInHand) {
        this.qtyInHand.set(qtyInHand);
    }

    @Column(name = "sellPrice")
    public double getSellPrice() {
        return sellPrice.get();
    }

    public DoubleProperty sellPriceProperty() {
        return sellPrice;
    }

    public void setSellPrice(double sellPrice) {
        this.sellPrice.set(sellPrice);
    }
}

Here product table structure

CREATE TABLE product
(
  idproduct serial NOT NULL,
  namefr character varying(50),
  qtyinhand double precision,
  sellprice double precision,
  CONSTRAINT product_pkey PRIMARY KEY (idproduct)
)

After making CRUD Operation using hibernate, and retrieve inserted records in database I see that hibernate is inserting negative value starting from -46, -45 ...... and so on.

Here is the connexion to database:

package util;

import model.Product;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class DatabaseUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.addAnnotatedClass(Product.class);
            return configuration.   buildSessionFactory(new StandardServiceRegistryBuilder().build());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("There was an error building the factor");
        }
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }

}

And here is my test:

import model.Product;
import org.hibernate.Session;
import util.DatabaseUtil;


public class Application {


    public static void main(String[] args) {
        Product p = new Product();

        p.setNameFr("Product 1");
        p.setQtyInHand(20);
        p.setSellPrice(101);

        Session session = DatabaseUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(p);
        session.getTransaction().commit();
        session.close();

    }
}

Upvotes: 2

Views: 3420

Answers (1)

Mykola Yashchenko
Mykola Yashchenko

Reputation: 5371

change to

@SequenceGenerator(name = "product_seq_gen", sequenceName = "product_idproduct_seq", initialValue = 1, allocationSize = 1)

Upvotes: 10

Related Questions