Hi
I'm using DB2 and Hibernate2.1 and I'm having trouble generating a sequence value for an id.
The code worked fine with Oracle but is failing with DB2.
Here is the hbm.xml file :
Hibernate version:
Hibernate 2.1
Mapping documents:
<class name="com.zz.zz.zz.zz.zz.impl.FormImpl" table="FORM">
<id name="FormID" type="java.lang.Long" unsaved-value="null">
<column name="FORM_ID" sql-type="INTEGER" />
<generator class="sequence">
<param name="sequence">FORM_ID_SEQ</param>
</generator>
</id>
<property name="DepartmentID" type="java.lang.Long" column="DEPARTMENT_ID"/>
</class>
Code between sessionFactory.openSession() and session.close():
public void add(Form form, String sData ) throws DAOException {
byte[] data = sData.getBytes();
Session session = null;
boolean autoCommit = false;
try {
session = SessionHelper.openSession();
form.setRepresentation(Hibernate.createBlob(new byte[1]));
session.save(form);
session.flush();
// save AutoCommit setting
autoCommit = session.connection().getAutoCommit();
session.connection().setAutoCommit(false);
session.refresh(form, LockMode.UPGRADE);
java.sql.Blob blob = form.getRepresentation();
OutputStream os = blob.setBinaryStream(Long.parseLong("1"));
os.write(data);
os.close();
session.connection().commit();
session.connection().setAutoCommit(autoCommit);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("FormDAO.add(form,sData) HibernateException " , e);
} finally {
try {
SessionHelper.close(session);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("FormDAO.add(form,sData) SessionHelper.close failed " , e);
}
}
}
Full stack trace of any exception that occurs:
Hibernate: values nextval for FORM_ID_SEQ
13:09:46,573 WARN JDBCExceptionReporter:38 - SQL Error: -84, SQLState: 42612
13:09:46,573 ERROR JDBCExceptionReporter:46 - [BEA][DB2 JDBC Driver][DB2]UNACCEPTABLE SQL STATEMENT
13:09:46,573 WARN JDBCExceptionReporter:38 - SQL Error: -84, SQLState: 42612
13:09:46,573 ERROR JDBCExceptionReporter:46 - [BEA][DB2 JDBC Driver][DB2]UNACCEPTABLE SQL STATEMENT
13:09:46,573 ERROR JDBCExceptionReporter:38 - Could not save object
java.sql.SQLException: [BEA][DB2 JDBC Driver][DB2]UNACCEPTABLE SQL STATEMENT
at weblogic.jdbc.base.BaseExceptions.createException(Unknown Source)
at weblogic.jdbc.base.BaseExceptions.getException(Unknown Source)
at weblogic.jdbc.db2.drda.DRDARequest.processSQLCA(Unknown Source)
at weblogic.jdbc.db2.drda.DRDARequest.processCodePoint(Unknown Source)
at weblogic.jdbc.db2.drda.DRDAStatementRequest.processCodePoint(Unknown Source)
at weblogic.jdbc.db2.drda.DRDAExecuteStatementRequest.processCodePoint(Unknown Source)
at weblogic.jdbc.db2.drda.DRDARequest.processReply(Unknown Source)
at weblogic.jdbc.db2.DB2ImplStatement.getNextResultType(Unknown Source)
at weblogic.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at weblogic.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at weblogic.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at weblogic.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at weblogic.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at weblogic.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at weblogic.jdbc.wrapper.PreparedStatement.executeQuery(PreparedStatement.java:92)
at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:69)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:727)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at com.zz.zz.zz.zz.zz.impl.hibernate.FormDAOImpl.add(FormDAOImpl.java:67)
Note that the statement :
Name and version of the database you are using:
DB2 8.1
The generated SQL (show_sql=true):
values nextval for FORM_ID_SEQ
--------------------------------------------
Note that the statement :
values nextval for FORM_ID_SEQ
works fine when executed in AQT, a DB GUI program where
the application and AQT usernames are the same.
Might a modification to the hbm.xml fix this problem?
(Like what?)
Ah ... I tried swapping out sequence with native as in :
<generator class="native">
<param name="sequence">FORM_ID_SEQ</param>
</generator>
But the database baulks saying it can't save because the id value is null.
|