我想在linf中有很多档案,有些档案可能有空链接。 我的重要休眠注释如下:
@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为零。可能这是非法的?但如果真的想要这个方案呢?
答案 0 :(得分:2)
正如我发现的那样,@ManyToMany
关系正是应该在这里使用的关系。如果signle对象属于两个不同的集合,那么在@OneToMany
的情况下会发生异常。
答案 1 :(得分:0)
请确保您正确使用MySQLDialect
,
此外,在向Archive
添加Linf
时,您在保存之前是setting the parent object
吗?
public void add(Archive archive){
if(this.archives == null){
this.archives = new HashSet<Archive>();
}
archive.setLinf(this); //setting the parent
this.archives.add(archive);
}
将MailSourceFile
添加到Archive
时相同?这需要照顾外国人口。