The table I'm mapping to, SOAAPI.core_patients_s is an insertable view. It calls a db function that inserts or updates a patient. This function takes a patient_ID field, if null it proceeds with an insert creating the ID inn it's own secret way, otherwise it does an update with the ID passed.
I'm trying to figure out how to map this. What kind of ID generator do I want?
If I set my ID to assigned, I can't proceed with a save without setting my ID, so that doesn't seem to work.
Sequence ID's require a key generator on the DB that I can access, so that doesn't seem to work either.
I've been trying to use the <sql-insert> but to pass NULL for the ID, but I've run into the following issue. Any help would be appreciated, thanks.
-Jeff
Hibernate version: 3.0.5
Mapping documents:
<class name="CorePatientsS" table="SOAAPI.core_patients_s">
<id name="patientId" column="patient_id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="java.lang.String" />
<property name="lastName" column="last_name" type="java.lang.String" />
<property name="applicationName" column="application_name" type="java.lang.String" />
<property name="accountId" column="account_id" type="java.lang.Integer" />
<sql-insert>INSERT INTO SOAAPI.core_patients_s (FIRST_NAME, LAST_NAME, APPLICATION_NAME, ACCOUNT_ID, PATIENT_ID) VALUES ( ?, ?, 'People', 569, NULL)</sql-insert>
</class>
Code between sessionFactory.openSession() and session.close():
Using Spring. calling getHibernateTemplate().save()
Full stack trace of any exception that occurs:
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not insert: [com.mms.domain.mms.CorePatientsS]; SQL [INSERT INTO SOAAPI.core_patients_s (FIRST_NAME, LAST_NAME, APPLICATION_NAME, ACCOUNT_ID, PATIENT_ID) VALUES ( ?, ?, 'People', 569, NULL)]; The column index is out of range: 3, number of columns: 2.; nested exception is org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:38)
at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:72)
at org.postgresql.jdbc2.AbstractJdbc2Statement.bindString(AbstractJdbc2Statement.java:2025)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1155)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1145)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:131)
at org.hibernate.type.StringType.set(StringType.java:24)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:39)
at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1617)
at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1594)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1850)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:48)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.prepareQueries(SessionImpl.java:895)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:885)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:852)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedParam(HibernateTemplate.java:843)
at com.mms.dao.hibernate.PatientDAOHibernateImpl.getPatientNamed(PatientDAOHibernateImpl.java:232)
at com.mms.dao.hibernate.PatientDAOHibernateImplTest.testSave(PatientDAOHibernateImplTest.java:60)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:
Postgres 8.0.0
The generated SQL (show_sql=true):
select nextval ('hibernate_sequence')
INSERT INTO SOAAPI.core_patients_s (FIRST_NAME, LAST_NAME, APPLICATION_NAME, ACCOUNT_ID, PATIENT_ID) VALUES ( ?, ?, 'People', 569, NULL)
Debug level Hibernate log excerpt:
5500 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#logOpenPreparedStatement : about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Hibernate: select nextval ('hibernate_sequence')
(5500 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#getPreparedStatement : preparing statement
(5500 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#logClosePreparedStatement : about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
(5500 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#closePreparedStatement : closing statement
(5531 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#logOpenPreparedStatement : about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Hibernate: INSERT INTO SOAAPI.core_patients_s (FIRST_NAME, LAST_NAME, APPLICATION_NAME, ACCOUNT_ID, PATIENT_ID) VALUES ( ?, ?, 'People', 569, NULL)
(5531 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#getPreparedStatement : preparing statement
(5531 ms) [main] DEBUG: org.hibernate.type.StringType#nullSafeSet : binding 'TESTFIRSTNAME' to parameter: 1
(5531 ms) [main] DEBUG: org.hibernate.type.StringType#nullSafeSet : binding 'TESTLAST' to parameter: 2
(5531 ms) [main] DEBUG: org.hibernate.type.StringType#nullSafeSet : binding 'People' to parameter: 3
(5531 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#logClosePreparedStatement : about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
(5531 ms) [main] DEBUG: org.hibernate.jdbc.AbstractBatcher#closePreparedStatement : closing statement
(5547 ms) [main] WARN : org.hibernate.util.JDBCExceptionReporter#logExceptions : SQL Error: 0, SQLState: 22023
(5547 ms) [main] ERROR: org.hibernate.util.JDBCExceptionReporter#logExceptions : The column index is out of range: 3, number of columns: 2.
(5547 ms) [main] ERROR: org.hibernate.event.def.AbstractFlushingEventListener#performExecutions : Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert: [com.mms.domain.mms.CorePatientsS]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
|