1
I am migrating my web application to the framework Hibernate
, but I have several requests SQL
with very specific parameters.
In one of these requests, I always need to include a new record in the database, delete the first 20 records from the database.
For this, I use the SQL string:
DELETE FROM USUARIO WHERE idemp = ? AND codrep = ? AND idcuf NOT IN (SELECT idcuf FROM (SELECT idcuf FROM USUARIO WHERE idemp = ? AND codrep = ? ORDER BY idcuf DESC LIMIT 20) t )
In my Hibernate code, I implemented the following method:
public void excluirRegistrosAntigos(UsuariosFVConexao usuario) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
String hql = "FROM "
+ "UsuariosFVConexao "
+ "WHERE "
+ "idemp = :idemp AND "
+ "codrep = :codrep AND "
+ "idcuf NOT IN "
+ "(SELECT idcuf "
+ "FROM "
+ "(SELECT idcuf "
+ "FROM "
+ "UsuariosFVConexao "
+ "WHERE "
+ "idemp = :idemp AND "
+ "codrep = :codrep "
+ "ORDER BY idcuf DESC LIMIT 20) t )";
try {
transaction = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter("idemp", usuario.getIdemp());
query.setParameter("codrep", usuario.getCodrep());
query.executeUpdate();
transaction.commit();
} catch (RuntimeException erro) {
if (transaction != null) {
transaction.rollback();
}
throw erro;
} finally {
session.close();
}
}
Class name changed in this case.
Anyway, in my tests, this method Hibernate
is returning me the following error:
ERROR: line 1:265: unexpected token: t
line 1:265: unexpected token: t
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:764)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:316)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:179)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
at br.com.portalserver.dao.UsuariosFVConexaoDAO.excluirRegistrosAntigos(UsuariosFVConexaoDAO.java:86)
at br.com.portalserver.dao.UsuariosFVConexaoDAOTeste.excluir(UsuariosFVConexaoDAOTeste.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:38)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:535)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
I don’t understand much yet this framework and so I can’t find the error. Thank you in advance.
Thank you very much
You have already tried to remove "t" from the query to see what happens?
– Marquezani
Hello Marquezani. Yes, I removed and it returns me the following error:
ERROR: line 1:255: unexpected token: LIMIT
– EduOK