gpt4 book ai didi

java - hibernate @OneToMany错误: Duplicate entry '0' for key 'PRIMARY'

转载 作者:行者123 更新时间:2023-12-02 17:14:37 26 4
gpt4 key购买 nike

我在两个实体 Pack and Board 之间存在一对多关系。当尝试使用存储库进行保存/更新时,我收到重复条目错误。由于某种原因,hibernate 正在尝试更新 Boards 的“id”字段,这些 Boards 是此关系中的子级(查询可以在下面的错误日志中看到)。

这是我正在使用的代码:

Pack.class

package com.vdts.lumberassets.domain;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="packs")
public class Pack extends AbstractEntity{
private SimpleStringProperty slug = new SimpleStringProperty();
private Load load;
private List<Board> boards = new ArrayList<>();
private Customer customer;
private Vendor vendor;
private DateTime dateTallied;
private DateTime dateCreated;
private DateTime dateModified;

@PrePersist
protected void onCreate() {
dateCreated = new DateTime();
dateModified = new DateTime();
}

@PreUpdate
protected void onUpdate() {
dateModified = new DateTime();
}

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
@JoinColumn(name="pack_id")
@IndexColumn(name="id")
public List<Board> getBoards() {
return boards;
}

@Transient
public ObservableList<Board> getBoardsObservable(){
return FXCollections.observableArrayList(boards);
}

public void setBoards(List<Board> boards) {
this.boards = boards;
}

public void addBoard(Board board){
this.boards.add(board);
}

@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="load_id")
public Load getLoad() {
return load;
}

public void setLoad(Load load) {
this.load = load;
}

@Column(name="slug")
public String getSlug() {
return slug.get();
}

public SimpleStringProperty slugProperty() {
return slug;
}

public void setSlug(String slug) {
this.slug.set(slug);
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="customer_id")
public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="vendor_id")
public Vendor getVendor() {
return vendor;
}

public void setVendor(Vendor vendor) {
this.vendor = vendor;
}

@Column(name="date_created", updatable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getDateCreated() {
return dateCreated;
}

public void setDateCreated(DateTime dateCreated) {
this.dateCreated = dateCreated;
}

@Column(name="date_modified")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getDateModified() {
return dateModified;
}

public void setDateModified(DateTime dateModified) {
this.dateModified = dateModified;
}

@Column(name="date_tallied")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getDateTallied() {
return dateTallied;
}

public void setDateTallied(DateTime dateTallied) {
this.dateTallied = dateTallied;
}
}

Board.class

package com.vdts.lumberassets.domain;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import javax.persistence.*;

@Entity
@Table(name="boards")
public class Board extends AbstractEntity {

private SimpleIntegerProperty surfaceMeasure = new SimpleIntegerProperty();

private Thickness thickness;

private Specie specie;

private Grade grade;

private Grader grader;

private Pack pack;

private SimpleDoubleProperty length = new SimpleDoubleProperty();

private SimpleIntegerProperty width = new SimpleIntegerProperty();

private DateTime dateCreated;

private DateTime dateModified;

private SimpleDoubleProperty price = new SimpleDoubleProperty();

@PrePersist
protected void onCreate() {
dateCreated = new DateTime();
dateModified = new DateTime();
}

@PreUpdate
protected void onUpdate() {
dateModified = new DateTime();
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="pack_id")
public Pack getPack() {
return pack;
}

public void setPack(Pack pack) {
this.pack = pack;
}

@Column(name="surface_measure")
public int getSurfaceMeasure() {
return surfaceMeasure.get();
}

public SimpleIntegerProperty surfaceMeasureProperty() {
return surfaceMeasure;
}

public void setSurfaceMeasure(int surfaceMeasure) {
this.surfaceMeasure.set(surfaceMeasure);
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="thickness_id")
public Thickness getThickness() {
return thickness;
}

public void setThickness(Thickness thickness) {
this.thickness = thickness;
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="specie_id")
public Specie getSpecie() {
return specie;
}

public void setSpecie(Specie specie) {
this.specie = specie;
}

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="grade_id")
public Grade getGrade() {
return grade;
}

public void setGrade(Grade grade) {
this.grade = grade;
}


@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="grader_id")
public Grader getGrader() {
return grader;
}

public void setGrader(Grader grader) {
this.grader = grader;
}

@Column(name="length")
public double getLength() {
return length.get();
}

public SimpleDoubleProperty lengthProperty() {
return length;
}

public void setLength(double length) {
this.length.set(length);
}

@Column(name="width")
public int getWidth() {
return width.get();
}

public SimpleIntegerProperty widthProperty() {
return width;
}

public void setWidth(int width) {
this.width.set(width);
}

@Column(name="date_created", updatable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getDateCreated() {
return dateCreated;
}

public void setDateCreated(DateTime dateCreated) {
this.dateCreated = dateCreated;
}

@Column(name="date_modified")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getDateModified() {
return dateModified;
}

public void setDateModified(DateTime dateModified) {
this.dateModified = dateModified;
}

@Column(name="price")
public double getPrice() {
return price.get();
}

public SimpleDoubleProperty priceProperty() {
return price;
}

public void setPrice(double price) {
this.price.set(price);
}
}

当尝试执行一些插入然后更新时,如下所示:

Pack pack = new Pack();
pack.addBoard(new Board());
pack.addBoard(new Board());
pack.addBoard(new Board());
ObservableList<Board> boards = pack.getBoardsObservable();
for (Board board : boards) {
board.setLength(13.33);
}
jpaPackRepository.save(pack);
for (Board board : boards) {
board.setLength(26.66);
}
jpaPackRepository.save(pack);

仅当 Boards 表为空时,第一次调用 jpaPackRepository.save() 才会起作用。第二次调用 jpaPackRepository.save() 将失败。

如果boards表不为空,则两者都不起作用。

这是我的存储库中的简单保存代码:

Session session = em.unwrap(Session.class);
session.saveOrUpdate(pack);
return pack;

最后,这是我收到的错误控制台消息:

Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: Duplicate entry '0' for key 'PRIMARY'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:188)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:154)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:519)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy55.save(Unknown Source)
at com.vdts.lumberassets.impl.testclasses.TestBoards.run(TestBoards.java:191)
at com.vdts.lumberassets.impl.RunLumberAssets.main(RunLumberAssets.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.hibernate.exception.ConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy72.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1260)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:279)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515)
... 15 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2376)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2360)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 31 more

最后,调试日志中这行奇怪的行:

21:28:55.699 [main] DEBUG o.h.p.c.AbstractCollectionPersister - Inserting collection: [com.vdts.lumberassets.domain.Pack.boards#27]
21:28:55.707 [main] DEBUG org.hibernate.SQL - update boards set pack_id=?, id=? where id=?

我完全不确定为什么 Hibernate 会尝试更新 id 字段,因为它是主键(由 Board 扩展的 AbstractEntity 类设置)。

非常感谢任何想法!我完全被难住了。

最佳答案

当您尝试添加现有主键 ID 0 的新行时,会出现此错误。因此,您可以通过添加 @GenerateValue(strategy=GenerationType.AUTO) 来避免重复相同的主键到实体类的主键。使用此功能,无论您提供什么作为主键,Hibernate 都会生成一个新的主键。

这是自动生成主键的示例。

   @Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

关于java - hibernate @OneToMany错误: Duplicate entry '0' for key 'PRIMARY' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097425/

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