Java 使用JPA和Hibernate插入记录时重复密钥

Java 使用JPA和Hibernate插入记录时重复密钥,java,hibernate,postgresql,jpa,Java,Hibernate,Postgresql,Jpa,你好。在开始编程之前,我正在测试我的数据库。在这个测试中,我有四个实体:员工、产品、销售账单和销售账单尾部 ER很简单(我还不能放图片): 计费的过程是这样的: 每份销售账单由一名员工制作 每份销售单都有很多细节 每个销售账单明细都有一个产品 员工 import java.io.Serializable; import javax.persistence.*; import java.util.Date; @Entity @Table(name="employees") @Nam

你好。在开始编程之前,我正在测试我的数据库。在这个测试中,我有四个实体:员工产品销售账单销售账单尾部

ER很简单(我还不能放图片):

计费的过程是这样的:

  • 每份销售账单由一名员工制作
  • 每份销售单都有很多细节
  • 每个销售账单明细都有一个产品

员工

import java.io.Serializable;

import javax.persistence.*;

import java.util.Date;


@Entity
@Table(name="employees")
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_employee")
    private Integer idEmployee;

    private String address;

    @Temporal(TemporalType.DATE)
    @Column(name="birth_date")
    private Date birthDate;

    @Temporal(TemporalType.DATE)
    @Column(name="created_at")
    private Date createdAt;

    @Column(name="dni", columnDefinition="bpchar")
    private String dni;

    private String email;

    private String names;

    @Column(name="state", columnDefinition="bpchar")
    private String state;

    private String surnames;

    @Temporal(TemporalType.DATE)
    @Column(name="updated_at")
    private Date updatedAt;

    public Employee() {
    }

    public Integer getIdEmployee() {
        return this.idEmployee;
    }

    public void setIdEmployee(Integer idEmployee) {
        this.idEmployee = idEmployee;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthDate() {
        return this.birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getDni() {
        return this.dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNames() {
        return this.names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getSurnames() {
        return this.surnames;
    }

    public void setSurnames(String surnames) {
        this.surnames = surnames;
    }

    public Date getUpdatedAt() {
        return this.updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }


}
产品

import java.io.Serializable;

import javax.persistence.*;

import java.math.BigDecimal;
import java.util.Date;


@Entity
@Table(name="products")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_product")
    private Integer idProduct;

    @Temporal(TemporalType.DATE)
    @Column(name="created_at")
    private Date createdAt;

    private String description;

    @Column(name="id_category")
    private Integer idCategory;

    private String name;

    private BigDecimal price;

    @Column(name="state", columnDefinition="bpchar")
    private String state;

    private Short stock;

    @Temporal(TemporalType.DATE)
    @Column(name="updated_at")
    private Date updatedAt;

    public Product() {
    }

    public Integer getIdProduct() {
        return this.idProduct;
    }

    public void setIdProduct(Integer idProduct) {
        this.idProduct = idProduct;
    }

    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getIdCategory() {
        return this.idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Short getStock() {
        return this.stock;
    }

    public void setStock(Short stock) {
        this.stock = stock;
    }

    public Date getUpdatedAt() {
        return this.updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }


}
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.jboss.logging.Logger;

public class Test {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("BillingTest");
        EntityManager em = null;
        EntityTransaction tx = null;
        final Logger logger = Logger.getLogger(Test.class.getName());

        try {
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();

            List<Product> products = em.createQuery("SELECT p FROM Product p")
                    .getResultList();
            List<Employee> employees = em.createQuery(
                    "SELECT e FROM Employee e").getResultList();

            SaleBill sale = new SaleBill();
            sale.setCompanyName("Centro de entrenamiento animal REX E.I.R.L");
            sale.setCompanyRuc("04844013110");
            sale.setSaleDate(new java.util.Date());
            sale.setState("1");
            sale.setEmployee(employees.get(0));

            SaleBillsDetail saleDetail = new SaleBillsDetail();
            List<SaleBillsDetail> details = new ArrayList<>();

            saleDetail.setSaleBill(sale);
            saleDetail.setProduct(products.get(0));
            saleDetail.setCuantity(Short.valueOf(3 + ""));
            details.add(saleDetail);

            saleDetail = new SaleBillsDetail();
            saleDetail.setSaleBill(sale);
            saleDetail.setProduct(products.get(1));
            saleDetail.setCuantity(Short.valueOf(5 + ""));
            details.add(saleDetail);

            sale.setSaleBillsDetails(details);
            em.persist(sale);
            em.flush();
            tx.commit();
        } catch (Exception ex) {
            logger.warn(ex.getMessage());
            System.exit(0);
        } finally {
            if(em != null) em.close();
            if(emf != null) emf.close();
        }

    }

}
SaleBill

import java.io.Serializable;

import javax.persistence.*;

import java.util.Date;
import java.util.List;


@Entity
@Table(name="sale_bills")
@NamedQuery(name="SaleBill.findAll", query="SELECT s FROM SaleBill s")
public class SaleBill implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_sale_bill")
    private Integer idSaleBill;

    @Column(name="company_name")
    private String companyName;

    @Column(name="company_ruc", columnDefinition="bpchar")
    private String companyRuc;

    @Temporal(TemporalType.DATE)
    @Column(name="sale_date")
    private Date saleDate;

    @Column(name="state", columnDefinition="bpchar")
    private String state;

    @ManyToOne(/*fetch = FetchType.EAGER,*/ optional=false)
    @JoinColumn(name="id_employee")
    private Employee employee;

    @OneToMany(cascade = CascadeType.ALL, /*fetch = FetchType.EAGER,*/ targetEntity = SaleBillsDetail.class)
    @JoinColumn(name="id_sale_bill_details")
    private List<SaleBillsDetail> saleBillsDetails;

    public SaleBill() {
    }

    public Integer getIdSaleBill() {
        return this.idSaleBill;
    }

    public void setIdSaleBill(Integer idSaleBill) {
        this.idSaleBill = idSaleBill;
    }

    public String getCompanyName() {
        return this.companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyRuc() {
        return this.companyRuc;
    }

    public void setCompanyRuc(String companyRuc) {
        this.companyRuc = companyRuc;
    }

    public Date getSaleDate() {
        return this.saleDate;
    }

    public void setSaleDate(Date saleDate) {
        this.saleDate = saleDate;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Employee getEmployee() {
        return this.employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public List<SaleBillsDetail> getSaleBillsDetails() {
        return this.saleBillsDetails;
    }

    public void setSaleBillsDetails(List<SaleBillsDetail> saleBillsDetails) {
        this.saleBillsDetails = saleBillsDetails;
    }

    public SaleBillsDetail addSaleBillsDetail(SaleBillsDetail saleBillsDetail) {
        getSaleBillsDetails().add(saleBillsDetail);
        saleBillsDetail.setSaleBill(this);

        return saleBillsDetail;
    }

    public SaleBillsDetail removeSaleBillsDetail(SaleBillsDetail saleBillsDetail) {
        getSaleBillsDetails().remove(saleBillsDetail);
        saleBillsDetail.setSaleBill(null);

        return saleBillsDetail;
    }

}
import java.io.Serializable;
import javax.persistence.*;


@Entity
@Table(name="sale_bills_details")
@NamedQuery(name="SaleBillsDetail.findAll", query="SELECT s FROM SaleBillsDetail s")
public class SaleBillsDetail implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_sale_bill_details")
    private Integer idSaleBillDetails;

    @Column(name="cuantity", nullable=false)
    private Short cuantity;

    @ManyToOne(/*fetch = FetchType.EAGER,*/ optional=false)
    @JoinColumn(name="id_product")
    private Product product;

    @ManyToOne
    @JoinColumn(name="id_sale_bill")
    private SaleBill saleBill;

    public SaleBillsDetail() {
    }

    public Integer getIdSaleBillDetails() {
        return this.idSaleBillDetails;
    }

    public void setIdSaleBillDetails(Integer idSaleBillDetails) {
        this.idSaleBillDetails = idSaleBillDetails;
    }

    public Short getCuantity() {
        return this.cuantity;
    }

    public void setCuantity(Short cuantity) {
        this.cuantity = cuantity;
    }

    public Product getProduct() {
        return this.product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public SaleBill getSaleBill() {
        return this.saleBill;
    }

    public void setSaleBill(SaleBill saleBill) {
        this.saleBill = saleBill;
    }

}
测试

import java.io.Serializable;

import javax.persistence.*;

import java.math.BigDecimal;
import java.util.Date;


@Entity
@Table(name="products")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_product")
    private Integer idProduct;

    @Temporal(TemporalType.DATE)
    @Column(name="created_at")
    private Date createdAt;

    private String description;

    @Column(name="id_category")
    private Integer idCategory;

    private String name;

    private BigDecimal price;

    @Column(name="state", columnDefinition="bpchar")
    private String state;

    private Short stock;

    @Temporal(TemporalType.DATE)
    @Column(name="updated_at")
    private Date updatedAt;

    public Product() {
    }

    public Integer getIdProduct() {
        return this.idProduct;
    }

    public void setIdProduct(Integer idProduct) {
        this.idProduct = idProduct;
    }

    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getIdCategory() {
        return this.idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return this.price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Short getStock() {
        return this.stock;
    }

    public void setStock(Short stock) {
        this.stock = stock;
    }

    public Date getUpdatedAt() {
        return this.updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }


}
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.jboss.logging.Logger;

public class Test {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("BillingTest");
        EntityManager em = null;
        EntityTransaction tx = null;
        final Logger logger = Logger.getLogger(Test.class.getName());

        try {
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();

            List<Product> products = em.createQuery("SELECT p FROM Product p")
                    .getResultList();
            List<Employee> employees = em.createQuery(
                    "SELECT e FROM Employee e").getResultList();

            SaleBill sale = new SaleBill();
            sale.setCompanyName("Centro de entrenamiento animal REX E.I.R.L");
            sale.setCompanyRuc("04844013110");
            sale.setSaleDate(new java.util.Date());
            sale.setState("1");
            sale.setEmployee(employees.get(0));

            SaleBillsDetail saleDetail = new SaleBillsDetail();
            List<SaleBillsDetail> details = new ArrayList<>();

            saleDetail.setSaleBill(sale);
            saleDetail.setProduct(products.get(0));
            saleDetail.setCuantity(Short.valueOf(3 + ""));
            details.add(saleDetail);

            saleDetail = new SaleBillsDetail();
            saleDetail.setSaleBill(sale);
            saleDetail.setProduct(products.get(1));
            saleDetail.setCuantity(Short.valueOf(5 + ""));
            details.add(saleDetail);

            sale.setSaleBillsDetails(details);
            em.persist(sale);
            em.flush();
            tx.commit();
        } catch (Exception ex) {
            logger.warn(ex.getMessage());
            System.exit(0);
        } finally {
            if(em != null) em.close();
            if(emf != null) emf.close();
        }

    }

}
我不明白为什么更新句子:updatesale\u bills\u details set id\u sale\u bill\u details=?其中id\u sale\u bill\u details=


谢谢你的阅读。感谢您的帮助。

删除
@JoinColumn
并在
SaleBill
类中添加
mappedBy

@OneToMany(cascade = CascadeType.ALL,
           /*fetch = FetchType.EAGER,*/
           targetEntity = SaleBillsDetail.class,
           mappedBy="saleBill")
private List<SaleBillsDetail> saleBillsDetails;
@OneToMany(cascade=CascadeType.ALL,
/*fetch=FetchType.EAGER*/
targetEntity=SaleBillsDetail.class,
mappedBy=“saleBill”)
私人清单销售明细;
@OneToMany(so@ManyToOne)映射应仅由关系的一方管理。
通过在
mappedBy
中使用saleBill,您告诉Hibernate此关系的所有者是SaleBillsDetail类中包含的名为saleBill的字段(该字段从saleBillsDetails字段类型中扣除,即
列表
)。只能在关系的@Many侧使用@JoinColumn

请在此处阅读更多信息:

如何生成ID?尝试将所有实体中的:@GeneratedValue(strategy=GenerationType.IDENTITY)更改为:@GeneratedValue(strategy=GenerationType.AUTO)。在postgresql中,我将smallserial用于自动递增PK。如果通过自动更改标识,则抛出SQLGramarException:
错误:错误:«休眠»序列»不存在位置:2005年3月17日,2015年11:27:55 AM com.company.billing.model.entities.Test main警告:org.hibernate.exception.sqlgrammareexception:无法提取结果集
能否尝试删除@JoinColumn并在@OneToMany(cascade=CascadeType.ALL,/*fetch=FetchType.EAGER,*/targetEntity=SaleBillsDetail.class,mappedBy=“saleBill”)中添加“mappedBy”私人列表销售明细?是 啊它起作用了。请将您的评论作为回应,选择它作为最佳答案。另外,请向我解释为什么mappedBy有效,而JoinColumn无效?谢谢米斯科!谢谢你,先生,我现在明白mappedBy和JoinColumn的区别了。再次感谢!