Hibernate和Postgresql否定主键id问题

时间:2016-01-22 10:56:59

标签: java database hibernate postgresql orm

我正在使用Hibernate 5.0.7和Postgresql DBMS。每个工作都很好,但我在postgresql数据库中遇到主键问题(负值)。

这是我的代码:

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);
    }
}

这里是产品表结构

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

使用hibernate进行CRUD操作,并在数据库中检索插入的记录后,我看到hibernate从-46,-45 ......开始插入负值等等。

以下是与数据库的联系:

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;
    }

}

这是我的测试:

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();

    }
}

1 个答案:

答案 0 :(得分:4)

更改为

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