简体   繁体   中英

Hibernate Exception while executing query

Following is my code snippet:

ApplicationContext ctx = new ClassPathXmlApplicationContext(
                    "classpath*:META-INF/spring/applicationContext*.xml");
            JpaTransactionManager jpatm = (JpaTransactionManager) ctx
                    .getBean("transactionManager");
            EntityManager em = jpatm.getEntityManagerFactory()
                    .createEntityManager();
String sqlQuery = "SELECT suc FROM SubUsrCont suc, UDMap uDMap WHERE suc.userid = uDMap.userid AND uDMap.parentuserid = :parentuserid";
TypedQuery<SubUsrCont> query = (TypedQuery<SubUsrCont>) em.createQuery(sqlQuery, SubUsrCont.class);
query.setParameter("parentuserid", parentid);
ArrayList<SubUsrCont> listContent = (ArrayList<SubUsrCont>) query.getResultList();

But when ever executed I get the following error:

[http-8080-1] ERROR org.hibernate.hql.PARSER - line 1:92: expecting OPEN, found '.'

Can anybody help???

Well, I found it and successfully tested it as well. It was due to my POJO package name. Previously it was in.myproject.myname . I changed it to com.myproject.myname . HQL was taking in as the SQL Keyword IN and was looking for OPEN '(' .

Why are you using a Native SQL query inside em.createQuery() ? This will not work because HQL can not have SELECT and furthermore you don't set the value for parameter :parentuserid .

String sqlQuery = "FROM SubUsrCont suc, UDMap uDMap WHERE suc.userid = uDMap.userid AND uDMap.parentuserid = :parentuserid";
TypedQuery<SubUsrCont> query = (TypedQuery<SubUsrCont>) em.createQuery(sqlQuery, SubUsrCont.class);
query.setParameter("parentuserid", <PARENT USER ID TO BE SEARCHED>);

Try this and see

If you would like to execute SQL use createNativeQuery . Also try "SELECT suc.* ...

Also you don't set the parameter :parentuserid

Use query setParameter("parentuserid",someValue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM