从excel

时间:2016-11-08 05:53:04

标签: java hibernate session batch-processing

我需要阅读excel和

  1. 如果数据不存在则插入行
  2. 如果数据存在则更新行
  3. 我在循环中做的是

    //Begin transaction
    //for(each row in excel){
    //  MyObject obj = readObject(row);
    //  session.saveOrUpdate(obj);
    /}
    //Close transaction
    

    我使用一个excel列将ID属性分配给POJO。 问题是excel中可能有多行具有相同的ID。所以我最终创建了具有相同ID的多个对象。然后,在调用 saveOrUpdate 时,我得到了 的 org.hibernate.NonUniqueObjectException

    我在StackOverflow上阅读过很多文章,但没有找到合适的方法。 我甚至试过这样做,但它不起作用

    //Begin transaction
    //for(row in excel){
    //  MyObject obj = readObject(row);
    //  session.saveOrUpdate(obj);
    //  session.flush();
    //  session.clear();
    /}
    //Close transaction
    

    还尝试了以下内容,但得到了 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException

    //Begin transaction
    //for(row in excel){
    //  MyObject obj = readObject(row);
    //  session.merge(obj);
    //  session.flush();
    //  session.clear();
    /}
    //Close transaction
    
    //Begin transaction
    //for(row in excel){
    //  MyObject obj = readObject(row);
    //  session.merge(obj);
    /}
    //Close transaction
    

    请帮忙。

    这是stackTraces中的一个:

    org.hibernate.exception.ConstraintViolationException:无法执行语句     在org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)     在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)     在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)     在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)     在org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)     在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2886)     在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3386)     在org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)     在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)     在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)     在org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)     在org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)     在com.nextscm.rms.db.dao.AbstractDao.flush(AbstractDao.java:51) 使用者:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目'owner1:warehouseid1:skuid98238:08-11-2016'用于键'PRIMARY'     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)     在com.mysql.jdbc.Util.getInstance(Util.java:387)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)     在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)     在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)     在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)     在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)     在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)     at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)     在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)     在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)     在org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)     在org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)     ......还有55个

0 个答案:

没有答案