gpt4 book ai didi

java - Hibernate 和 Postgresql 负主键 id 问题

转载 作者:行者123 更新时间:2023-11-29 14:06:40 25 4
gpt4 key购买 nike

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

}
}

最佳答案

改为

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

关于java - Hibernate 和 Postgresql 负主键 id 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com