Hi everyone.
I have to import CSV file into DB, every line of csv file represents one record in table.
the table has two naturial key fileds and no version field.
1.What is the proper way to do this type of job?
2.Why I can' explicitly update the table?
when I execute explicitly session.update(bean),Hibernate always issue the insert statement.
the
Description bean = new Description();
this.parseBean(bean, line);
try {
session.save(bean);
session.flush();
} catch (JDBCException e) {
if (e.getErrorCode() == -803) {
// that means there is already one row in table with the same key
session.update(bean);
session.flush();
}
}
return bean;
------------------------------------
Hibernate version:
2.1.8
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="jp.co.fuyoauto.batch.bean">
<class name="Description" table="DESCRIPTION">
<composite-id class="DESCRIPTIONPK" name="Id">
<key-property
column="CODE"
name="Code"
type="string"
/>
<key-property
column="CODE_DIVISION"
name="CodeDivision"
type="string"
/>
</composite-id>
<property
column="UPDATE_TIME"
length="8"
name="UpdateTime"
not-null="false"
type="time"
/>
<property
column="CODE_NAME"
length="82"
name="CodeName"
not-null="false"
type="string"
/>
<property
column="REG_DT"
length="10"
name="RegDt"
not-null="false"
type="date"
/>
<property
column="REG_TIME"
length="8"
name="RegTime"
not-null="false"
type="time"
/>
<property
column="UPDATE_DT"
length="10"
name="UpdateDt"
not-null="false"
type="date"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Description bean = new Description();
this.parseBean(bean, line);
try {
session.save(bean);
session.flush();
} catch (JDBCException e) {
if (e.getErrorCode() == -803) {
session.update(bean);
session.flush();
}
}
tx.commit();
Full stack trace of any exception that occurs:
2005/03/02 14:34:07 net.sf.hibernate.cfg.Environment <clinit>
情報: Hibernate 2.1.8
2005/03/02 14:34:07 net.sf.hibernate.cfg.Environment <clinit>
情報: hibernate.properties not found
2005/03/02 14:34:07 net.sf.hibernate.cfg.Environment <clinit>
情報: using CGLIB reflection optimizer
2005/03/02 14:34:07 net.sf.hibernate.cfg.Environment <clinit>
情報: using JDK 1.4 java.sql.Timestamp handling
2005/03/02 14:34:07 net.sf.hibernate.cfg.Configuration addClass
情報: Mapping resource: jp/co/fuyoauto/batch/bean/Description.hbm.xml
2005/03/02 14:34:08 net.sf.hibernate.cfg.Binder bindRootClass
情報: Mapping class: jp.co.fuyoauto.batch.bean.Description -> DESCRIPTION
2005/03/02 14:34:08 net.sf.hibernate.cfg.Configuration secondPassCompile
情報: processing one-to-many association mappings
2005/03/02 14:34:08 net.sf.hibernate.cfg.Configuration secondPassCompile
情報: processing one-to-one association property references
2005/03/02 14:34:08 net.sf.hibernate.cfg.Configuration secondPassCompile
情報: processing foreign key constraints
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
警告: No dialect set - using GenericDialect: Dialect class not found: org.hibernate.dialect.DB2Dialect
2005/03/02 14:34:08 net.sf.hibernate.dialect.Dialect <init>
情報: Using dialect: net.sf.hibernate.dialect.GenericDialect
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: Use outer join fetching: true
2005/03/02 14:34:08 net.sf.hibernate.connection.UserSuppliedConnectionProvider configure
警告: No connection properties specified - the user must supply JDBC connections
2005/03/02 14:34:08 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
情報: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: Use scrollable result sets: false
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: Use JDBC3 getGeneratedKeys(): false
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: Optimize cache for minimal puts: false
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: echoing all SQL to stdout
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: Query language substitutions: {}
2005/03/02 14:34:08 net.sf.hibernate.cfg.SettingsFactory buildSettings
情報: cache provider: net.sf.hibernate.cache.EhCacheProvider
2005/03/02 14:34:08 net.sf.hibernate.cfg.Configuration configureCaches
情報: instantiating and configuring caches
2005/03/02 14:34:08 net.sf.ehcache.config.Configurator configure
警告: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/software/Hibernate/hibernate-3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
2005/03/02 14:34:08 net.sf.hibernate.impl.SessionFactoryImpl <init>
情報: building session factory
2005/03/02 14:34:09 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
情報: Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into DESCRIPTION (UPDATE_TIME, CODE_NAME, REG_DT, REG_TIME, UPDATE_DT, CODE, CODE_DIVISION) values (?, ?, ?, ?, ?, ?, ?)
2005/03/02 14:34:13 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: -803, SQLState: 23505
2005/03/02 14:34:13 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
致命的: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;DB2INST.DESCRIPTION
2005/03/02 14:34:13 net.sf.hibernate.impl.SessionImpl execute
致命的: Could not synchronize database state with session
Hibernate: insert into DESCRIPTION (UPDATE_TIME, CODE_NAME, REG_DT, REG_TIME, UPDATE_DT, CODE, CODE_DIVISION) values (?, ?, ?, ?, ?, ?, ?)
2005/03/02 14:34:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: -803, SQLState: 23505
2005/03/02 14:34:16 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
致命的: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;DB2INST.DESCRIPTION
2005/03/02 14:34:16 net.sf.hibernate.impl.SessionImpl execute
致命的: Could not synchronize database state with session
net.sf.hibernate.exception.ConstraintViolationException: could not insert: [jp.co.fuyoauto.batch.bean.Description#jp.co.fuyoauto.batch.bean.DESCRIPTIONPK@72231ec6]
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:62)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:472)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at jp.co.fuyoauto.batch.dao.DescriptionParser.initBean(DescriptionParser.java:83)
at jp.co.fuyoauto.batch.dao.CSVBatch.processImport(CSVBatch.java:170)
at jp.co.fuyoauto.batch.dao.DescriptionParser.main(DescriptionParser.java:35)
Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;DB2INST.DESCRIPTION
at com.ibm.db2.jcc.b.zc.d(zc.java:1351)
at com.ibm.db2.jcc.a.db.l(db.java:366)
at com.ibm.db2.jcc.a.db.a(db.java:64)
at com.ibm.db2.jcc.a.r.a(r.java:48)
at com.ibm.db2.jcc.a.tb.c(tb.java:266)
at com.ibm.db2.jcc.b.ad.Z(ad.java:1666)
at com.ibm.db2.jcc.b.ad.d(ad.java:2224)
at com.ibm.db2.jcc.b.ad.V(ad.java:521)
at com.ibm.db2.jcc.b.ad.executeUpdate(ad.java:504)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:462)
... 9 more
Name and version of the database you are using:
db2 8.1
The generated SQL (show_sql=true):
Hibernate: insert into DESCRIPTION (UPDATE_TIME, CODE_NAME, REG_DT, REG_TIME, UPDATE_DT, CODE, CODE_DIVISION) values (?, ?, ?, ?, ?, ?, ?)
Debug level Hibernate log excerpt:
not set
|