Can anyone please point me in the right direction on how to skip existing database rows when doing session.save() without having check for existing records first? I am using the following code:
pointLogs has many (~10000) values and may contain duplicates of existing records.
Code:
try {
tx = session.beginTransaction();
long i = 0;
for (PointLog pointLog : pointLogs) {
try {
session.save(pointLog);
}
catch (Exception e) {
logger.warning("Error saving " + pointLog.getId() + ":" + e.getLocalizedMessage());
}
if (++i % DAO.JDBC_BATCH_SIZE == 0) {
// flush a batch of inserts and release memory
session.flush();
session.clear();
}
}
tx.commit();
}
finally {
session.close();
}
I am seeing exceptions as expected however the insert fails.
Hibernate version: 3.3.1GA
Mapping documents:annotated
Code between sessionFactory.openSession() and session.close():as above
Full stack trace of any exception that occurs:Quote:
WARNING: SQL Error: 0, SQLState: null
21/10/2008 11:28:01 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Batch entry 19 insert into hmsPointLog (state, value, logTime, serial, tag) values (1, 0.7000000000000001, 1223023107288, 732813, FOO) was aborted. Call getNextException to see the cause.
21/10/2008 11:28:01 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 23505
21/10/2008 11:28:01 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ERROR: duplicate key value violates unique constraint "hmspointlog_pkey"
21/10/2008 11:28:01 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [pmm.hms.web.model.point.PointLog]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2295)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at pmm.hms.web.dao.PointDAO.persistPointLogs(Unknown Source)
at pmm.hms.web.remoting.HistoryPoller.getPointLogs(Unknown Source)
at pmm.hms.web.remoting.HistoryPoller.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.BatchUpdateException: Batch entry 19 insert into hmsPointLog (state, value, logTime, serial, tag) values (1, 0.7000000000000001, 1223023107288, 732813, FOO) was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2275)
... 12 more
Name and version of the database you are using:Postgres 8.3
The generated SQL (show_sql=true):Debug level Hibernate log excerpt:[/code]