Hi,
Derby 10.6.1 officially supports sequences. (See JIRA-Task:
https://issues.apache.org/jira/browse/DERBY-712, Docs:
https://issues.apache.org/jira/browse/DERBY-4568).
I tried to use this feature with hibernate, but got this error:
Code:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not get next sequence value; nested exception is org.hibernate.exception.SQLGrammarException: could not get next sequence value
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:487)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.springframework.dao.InvalidDataAccessResourceUsageException: could not get next sequence value; nested exception is org.hibernate.exception.SQLGrammarException: could not get next sequence value
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:613)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:377)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
dao.impl.HibernateScoreDaoImpl.saveScore(HibernateScoreDaoImpl.java:29)
service.impl.ScoreServiceImpl.saveScore(ScoreServiceImpl.java:35)
webapp.controller.GeckoHuntController.handleRequest(GeckoHuntController.java:59)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.hibernate.exception.SQLGrammarException: could not get next sequence value
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:132)
org.hibernate.id.SequenceHiLoGenerator$1.getNextValue(SequenceHiLoGenerator.java:84)
org.hibernate.id.enhanced.OptimizerFactory$LegacyHiLoAlgorithmOptimizer.generate(OptimizerFactory.java:351)
org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:81)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:635)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
dao.impl.HibernateScoreDaoImpl.saveScore(HibernateScoreDaoImpl.java:29)
service.impl.ScoreServiceImpl.saveScore(ScoreServiceImpl.java:35)
webapp.controller.GeckoHuntController.handleRequest(GeckoHuntController.java:59)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.sql.SQLSyntaxErrorException: Syntaxfehler: Encountered "seq_score" at line 1, column 20.
org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:110)
org.hibernate.id.SequenceHiLoGenerator$1.getNextValue(SequenceHiLoGenerator.java:84)
org.hibernate.id.enhanced.OptimizerFactory$LegacyHiLoAlgorithmOptimizer.generate(OptimizerFactory.java:351)
org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:81)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:635)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
dao.impl.HibernateScoreDaoImpl.saveScore(HibernateScoreDaoImpl.java:29)
service.impl.ScoreServiceImpl.saveScore(ScoreServiceImpl.java:35)
webapp.controller.GeckoHuntController.handleRequest(GeckoHuntController.java:59)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.apache.derby.client.am.SqlException: Syntaxfehler: Encountered "seq_score" at line 1, column 20.
org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:110)
org.hibernate.id.SequenceHiLoGenerator$1.getNextValue(SequenceHiLoGenerator.java:84)
org.hibernate.id.enhanced.OptimizerFactory$LegacyHiLoAlgorithmOptimizer.generate(OptimizerFactory.java:351)
org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:81)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:635)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
dao.impl.HibernateScoreDaoImpl.saveScore(HibernateScoreDaoImpl.java:29)
service.impl.ScoreServiceImpl.saveScore(ScoreServiceImpl.java:35)
webapp.controller.GeckoHuntController.handleRequest(GeckoHuntController.java:59)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Solution is (imho):
org.hibernate.dialect.DerbyDialect needs to implement getSequenceNextValString as follows
Code:
public String getSequenceNextValString(String sequenceName) {
return "values next value for " + sequenceName;
}
instead of
Code:
public String getSequenceNextValString(String sequenceName) {
return "values nextval for " + sequenceName;
}
Am I right? (My sample app worked fine after modifying field "sql" in "SequenceGenerator")
If yes, how to add this code officially to hibernate?
Maybe, there is more to do than only this change!? Is there already progress on this topic?
Greets from Germany, Sebastian