5

I am trying to execute a select statement in JPA which is throwing me the above error. The code is:

TypedQuery<ListPersonsObj> typedQuery = null;
String query = "";
List<ListPersonsObj> list = null;
if (some condition) {
   query = "SELECT NEW in.healthelife.DGS.dao.ListPersonsObj"
                    + "(c.personId, c.givenName, c.middleName, c.address1, "
                    + "c.address2, c.dateOfBirth, "
                    + "c.phoneNumber, c.email) FROM Patients c";
   typedQuery = EntityManagerUtil.getEntityManager()
                   .createQuery(query, ListPersonsObj.class);
   list = typedQuery.getResultList();
}

The ListPersonsObj class:

public class ListPersonsObj {

   private Long personId;
   private String givenName;
   private String middleName;
   private String address1;
   private String address2;
   private String dateOfBirth;
   private String phoneNumber;
   private String email;

   public ListPersonsObj() {
   }

   public ListPersonsObj(Long personId, String givenName, String middleName,
         String address1, String address2,
         String dateOfBirth, String phoneNumber, String email) {
      this.personId = personId;
      this.givenName = givenName;
      this.middleName = middleName;
      this.address1 = address1;
      this.address2 = address2;
      this.dateOfBirth = dateOfBirth;
      this.phoneNumber = phoneNumber;
      this.email = email;
   }

   public ListPersonsObj(ListPersonsObj list) {
      try {
         BeanUtils.copyProperties(this, list);
      } catch (IllegalAccessException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   // .. getters & setters 
}

Exception caught

java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.FromReferenceNode

Can anyone please shed some light on this.

2
  • Give the whole exception stack trace and comment line numbers in to code where it happens. Commented Nov 24, 2017 at 6:54
  • @pirho The problem is in package name (in.healthelife.*). I have changed it to com.healthelife and now it works. Commented Nov 24, 2017 at 7:15

1 Answer 1

7

One reason for this happening is that Hibernate finds a reserved word in a wrong place in JPQL.

For this case

The problem is with package name

...

The prefix is 'in' instead of 'com'. While parsing the query the 'in' is treated as reserved world

For reference, see this

Related issue in Stack Overflow here

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I found out just a while ago. Thanks for the reply though. I have changed the package name and it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.