-3
query=entityManager.createQuery(" select a.birthDate from PersonalDetailsDomain a where a.empCode=(select b.empCode from User b where b.userId=?)");
    query.setParameter(1,userId);
    Date dob =(Date)query.getSingleResult();

Excetion:

java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: empCode of: com.erp.hrms.User [ select a.birthDate from com.erp.hrms.newjoinee.PersonalDetailsDomain a where a.empCode=(select b.empCode from com.erp.hrms.User b where b.userId=?)]

  • 1
    Does your "User" entity have an "empCode" property with getters and setters? – mael Jul 25 '13 at 06:46
  • As you are writing Pure SQL Query not the HSQL , you have to given the table name not the POJO Class name of that individual table –  Jul 25 '13 at 06:47
  • 1
    Have you looked at these other questions about jpa & subquries: http://stackoverflow.com/questions/13460235/jpa-2-criteria-using-in-and-subqueries http://stackoverflow.com/questions/11187188/jpa-criteria-query-with-subquery http://stackoverflow.com/questions/4662336/subquery-in-select-clause http://stackoverflow.com/questions/4483576/jpa-2-0-criteria-api-subqueries-in-expressions http://stackoverflow.com/questions/7269010/jpa-hibernate-subquery-in-from-clause – Trevor Gowing Jul 25 '13 at 06:47

2 Answers2

0

Use ':userId' instead of '?' and also use method createSQLQuery for native sql queries.

query=entityManager.createSQLQuery(" select a.birthDate from PersonalDetailsDomain a where a.empCode=(select b.empCode from User b where b.userId= :userId)");

query.setParameter("userId", userId);

Date dob =(Date)query.getSingleResult();
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

Are you sure to have correctly mapped the field (or relative accessor) for User.empCode?
Attach User definition, would help.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69