Hi All,
I'm having this wee problem with accessing a stored procedure via an IdentifierGenerator implemented class, to obviously get the unique id of the table that I'm currently dealing with. I'm using annotations which seems to complicate things further (mainly due to the fact that I'm fairly new to hibernate I'd imagine). I realise that it's an issue with the named query not being found, but I'm not sure how to rectify it. Anyways, following is my class, and following that is the stacktrace of the problem - any help would be greatly received.
Code:
@NamedNativeQuery(name="getNextId",
query="? = call usp_getNextId (:tableName)",
resultClass = Integer.class)
public class IdGenerator implements IdentifierGenerator, Configurable {
private String tableName;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void configure(Type type, Properties properties, Dialect dialect) throws MappingException {
setTableName(properties.getProperty(PersistentIdentifierGenerator.TABLE));
}
public Serializable generate(SessionImplementor session, Object obj)
throws HibernateException {
return getNextNumber(session);
}
private int getNextNumber(SessionImplementor session) {
String storedProcedure = "getNextId";
Integer nextValue = 0;
try {
PreparedStatement statement =
session.getBatcher().prepareSelectStatement(storedProcedure);
statement.setString(1, tableName);
try {
ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
nextValue = Integer.parseInt(resultSet.getString(1));
}
} finally {
resultSet.close();
}
} finally {
session.getBatcher().closeStatement(statement);
}
} catch (SQLException e) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), e,
"could not fetch initial value for increment generator",
storedProcedure);
}
return nextValue;
}
}
---------------------------
org.hibernate.MappingException: Named query not known: getNextId
at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1288)
at com.wraith.money.model.db.schema.IdGenerator.getNextNumber(IdGenerator.java:53)
at com.wraith.money.model.db.schema.IdGenerator.generate(IdGenerator.java:43)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:740)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
at com.wraith.money.model.db.metadata.dao.GenericDAOHibernateImpl.saveUpdate(GenericDAOHibernateImpl.java:109)
at com.wraith.money.model.db.metadata.Metadata.saveCustomTable(Metadata.java:84)
at com.wraith.money.model.db.schema.CustomTableEntityTest.testInsertCustomTables(CustomTableEntityTest.java:44)
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 com.intellij.rt.junit4.Junit4ClassSuite.run(Junit4ClassSuite.java:99)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at com.intellij.rt.execution.junit.IdeaTestRunner.doRun(IdeaTestRunner.java:94)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at com.intellij.rt.execution.junit.IdeaTestRunner.startRunnerWithArgs(IdeaTestRunner.java:22)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:118)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
Hibernate: getNextId
Many Thanks,
R.