How do I write hql query with cast?

HQL supports CAST (if underlying database supports it), you can use it:

select a.id as id,a.name as name,b.address as address 
from Personal as a,Home as b
where cast(a.id as string) = b.studid 

See also:

  • 16.10. Expressions

You really need to think why have you got a need to join two entities by properties of different types. Most likely it suggests that some of the entities need to be refactored, which could include changing data types for columns of the underlying db tables. If the model is correct there will be no need to twist Hibernate.


Just noticed that you are using JPA, there you can not cast or convert datatpes. In the query language, only values of the same type can be compared! read in http://download.oracle.com/javaee/5/tutorial/doc/bnbuf.html#bnbvu