My question comes from
section 5.1.2.4. Partial identifier generation.
The reference manual states:
Quote:
You can also generate properties inside an @EmbeddedId class.
That is what I want to do.
I have a table as follows:
Code:
CREATE TABLE gen_test (
rateno VARCHAR(20) NOT NULL,
ratefiltno numeric(20) identity NOT NULL
)
ALTER TABLE gen_test
ADD CONSTRAINT gen_test_pk PRIMARY KEY (
ratefiltno,
rateno
)
Here is the entity and it's id:
Code:
@Entity()
@Table(name="GEN_TEST")
@Proxy(lazy = false)
public class GenTest extends AbstractPersistent<GenTest> implements java.io.Serializable {
private GenTestId id;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="ratefiltno", column=@Column(name="RATEFILTNO", nullable=false, precision=20, scale=0) ),
@AttributeOverride(name="rateno", column=@Column(name="RATENO", nullable=false, length = 20) ) } )
public GenTestId getId() {
return this.id;
}
public void setId(GenTestId id) {
this.id = id;
}
// define equals and hashCode
}
@Embeddable
public class GenTestId implements java.io.Serializable {
private Integer ratefiltno;
private String rateno;
public GenTestId() {
}
public GenTestId(Integer ratefiltno, String rateno) {
this.ratefiltno = ratefiltno;
this.rateno = rateno;
}
@Column(name = "RATEFILTNO", nullable = false, precision=20, scale=0)
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getRatefiltno() {
return this.ratefiltno;
}
public void setRatefiltno(Integer ratefiltno) {
this.ratefiltno = ratefiltno;
}
@Column(name = "RATENO", nullable = false, length = 20)
public String getRateno() {
return rateno;
}
public void setRateno(String rateno) {
this.rateno = rateno;
}
// define equals and hashCode
}
I want to be able to insert rateNo by manually assigning it and ratefiltno using a identity generator. I try to insert after populating the entity as such:
Code:
GenTest entity = getBean(GenTest.class);
GenTestId id = new GenTestId();
id.setRateno("9");
entity.setId(id);
getHibernateTemplate().saveOrUpdate(entity);
I am connecting to a SQL Server database. I have this working when there is no rateNo column. I am using Spring and the above code is being executed in a method marked @Transactional. Thus, the saveOrUpdate() is occurring in a transaction. I believe this is required? I get the exception:
Code:
ERROR [main] (JDBCExceptionReporter.java:234) - Cannot insert explicit value for identity column in table 'gen_test' when IDENTITY_INSERT is set to OFF.
ERROR [main] (AbstractFlushingEventListener.java:324) - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert: [com.geowareinc.finance.rate.models.GenTest]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2436)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2856)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at com.geowareinc.common.persistent.ChainedTransactionManager.commit(ChainedTransactionManager.java:76)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.geowareinc.transactionalservice.etl.persistent.TransactionalService$$EnhancerByCGLIB$$86a6373b.testSave(<generated>)
at com.geowareinc.transactionalservice.etl.persistent.TransactionalServiceTest.testSave(TransactionalServiceTest.java:23)
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:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'gen_test' when IDENTITY_INSERT is set to OFF.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:308)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2416)
I did a google search for:
Code:
Cannot insert explicit value for identity column in table 'gen_test' when IDENTITY_INSERT is set to OFF
and found that I should run this in SQL Server Management Studio:
Code:
SET IDENTITY_INSERT gen_test ON;
but that did nothing.
Any help would be very appreciated.