3

我想在 linf 中有很多档案,有些档案可能有空 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 为零。这可能是非法的吗?但是如果真的想要这个方案呢?

4

2 回答 2

3

正如我发现的那样,@ManyToMany关系正是应该在这里使用的。如果 signle 对象属于两个不同的集合,则在 case 中会发生上述异常@OneToMany

于 2012-11-10T18:55:27.397 回答
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吗?这是照顾外来重点人群所必需的。

于 2012-11-09T18:40:28.043 回答