kpdunn wrote:
I have searched the FAQs and this forum so apologies if this is a common question. I have a DB2 (v8.2) table whose primary key is defined as follows
IND_ID integer primary key GENERATED ALWAYS AS IDENTITY
I have tried and failed to save the corresponding bean, no matter what I try. I have pasted the relevant snippets and log entries below. If anyone has experienced this problem and overcome it I would be most grateful if you could share your expertise.
1.
<id name="id" type="int" column="IND_ID"><generator class="identity"/></id>
Result: SQLException
"A value cannot be specified for column "IND_ID" which is defined as GENERATED ALWAYS"
2.
<id name="id" type="int" column="IND_ID"><generator class="native"/></id>
Result: SQLException
"KDUNN.HIBERNATE_SEQUENCE" is an undefined name.
3.
<id name="id" type="int" column="IND_ID"><generator class="increment"/></id>
Result: SQLException
A value cannot be specified for column "IND_ID" which is defined as GENERATED ALWAYS.
4.
<id name="id" type="int" column="IND_ID"><generator class="select"/></id>
Result: ear doesn't even start up but gives this message- org.hibernate.id.IdentifierGenerationException: no natural-id property defined; need to specify [key] in generator parameters
5.
<id name="id" column="IND_ID" type="int">
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
<param name="force_table_use">true</param>
<param name="value_column">IND_ID</param>
</generator>
</id>
Result:
org.hibernate.HibernateException: error performing isolated work
at org.hibernate.engine.transaction.Isolater$JtaDelegate.delegateWork(Isolater.java:142)
at org.hibernate.engine.transaction.Isolater.doIsolatedWork(Isolater.java:40)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:51)
at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:72)
at org.hibernate.id.enhanced.OptimizerFactory$NoopOptimizer.generate(OptimizerFactory.java:90)
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:157)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at
Hi,
You can try using the following :
<generator class="sequence">
<param name="sequence">IND_ID</param>
</generator>
Just add a surrogate key column to your table which will be uniquely identifying your table and then add corresponding column to your bean and mapping files as an id field..