Get Max value from table in hibernate -
i using below java(1.7) function max value hsql using hibernate 4.3:
public int getmaxid() { session session = hibernateutil.getsessionfactory().opensession(); query q = session.createquery("select cast(s.id integer) student s order cast(s.id integer) desc"); list result = q.list(); return integer.parseint(result.get(0).tostring()); }
but getting below error:
error: unexpected token: ) required: in statement [select cast(student0_.idasinteger) col_0_0_ public.public.student student0_ order cast(student0_.id integer)desc] exception in thread "main" org.hibernate.exception.sqlgrammarexception: not prepare statement @ org.hibernate.exception.internal.sqlexceptiontypedelegate.convert(sqlexceptiontypedelegate.java:80) @ org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconverter.java:49) @ org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:126) @ org.hibernate.engine.jdbc.internal.statementpreparerimpl$statementpreparationtemplate.preparestatement(statementpreparerimpl.java:196) @ org.hibernate.engine.jdbc.internal.statementpreparerimpl.preparequerystatement(statementpreparerimpl.java:160) @ org.hibernate.loader.loader.preparequerystatement(loader.java:1884) @ org.hibernate.loader.loader.executequerystatement(loader.java:1861) @ org.hibernate.loader.loader.executequerystatement(loader.java:1838) @ org.hibernate.loader.loader.doquery(loader.java:909) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:354) @ org.hibernate.loader.loader.dolist(loader.java:2551) @ org.hibernate.loader.loader.dolist(loader.java:2537) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2367) @ org.hibernate.loader.loader.list(loader.java:2362) @ org.hibernate.hql.internal.classic.querytranslatorimpl.list(querytranslatorimpl.java:939) @ org.hibernate.engine.query.spi.hqlqueryplan.performlist(hqlqueryplan.java:229) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1260) @ org.hibernate.internal.queryimpl.list(queryimpl.java:103) @ test.hservice.getmaxid(hservice.java:23) @ test.test.main(test.java:19) caused by: java.sql.sqlsyntaxerrorexception: unexpected token: ) required: in statement [select cast(student0_.idasinteger) col_0_0_ public.public.student student0_ order cast(student0_.id integer)desc] @ org.hsqldb.jdbc.jdbcutil.sqlexception(unknown source) @ org.hsqldb.jdbc.jdbcutil.sqlexception(unknown source) @ org.hsqldb.jdbc.jdbcpreparedstatement.<init>(unknown source) @ org.hsqldb.jdbc.jdbcconnection.preparestatement(unknown source) @ org.hibernate.engine.jdbc.internal.statementpreparerimpl$5.doprepare(statementpreparerimpl.java:162) @ org.hibernate.engine.jdbc.internal.statementpreparerimpl$statementpreparationtemplate.preparestatement(statementpreparerimpl.java:186) ... 16 more
if remove cast(s.id integer) select statement only, query works fine e.g.
query q = session.createquery("select s.id student s order cast(s.id integer) desc");
i have use cast id varchar type column.
Comments
Post a Comment