gpt4 book ai didi

java - ConstraintViolationException : Duplicate entry

转载 作者:行者123 更新时间:2023-11-29 02:30:55 25 4
gpt4 key购买 nike

我想在 linf 中有很多文件,有些文件可能有空 linf。我的基本 hibernate 注释如下:

@Entity
public class Linf {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Set<Archive> getArchive() {
return archives;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public long getId() {
return this.id;
}
}

@Entity
public class Archive {
public Archive() {
}

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Linf getLinf() {
return linf;
}

public void setLinf(Linf linf) {
this.linf = linf;
}


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public long getId() {
return this.id;
}


}

当我通过 sess.saveOrUpdate(linfHbm) 保存 Linf 对象时,我得到

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Duplicate entry '1' for key 'archive_ID'
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 $Proxy21.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:1210)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:399)
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.kriyak.utils.ImportLinfs.main(ImportLinfs.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'archive_ID'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2347)
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 20 more

最后一个导致进程崩溃的 sql 命令是:

插入 Linf_Archive (Linf_ID, archive_ID) 值 (?, ?)

此表中的两个字段都不能为空。

知道为什么会发生这种情况吗?

PS:我已经隔离了导致崩溃的代码。这是产生问题的简单场景:

Session sess = getSessionFactory().getCurrentSession();
sess.beginTransaction();
Archive a = new Archive();
Linf linf1 = new Linf();
Linf linf2 = new Linf();
linf1.setTitle("aa1");
linf2.setTitle("aa2");
linf1.getArchive().add(a);
linf2.getArchive().add(a);
sess.save(linf1);
sess.save(linf2);
sess.getTransaction().commit();

请注意存档的 linf 为零。这可能是非法的?但是如果真的想要这个方案怎么办?

最佳答案

正如我发现的那样,@ManyToMany 关系正是应该在此处使用的关系。如果 signle 对象属于两个不同的集合,那么在 @OneToMany 的情况下会发生上述异常。

关于java - ConstraintViolationException : Duplicate entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13313922/

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