I need to combine 2 tables using hql, both are having common column, but table1
common column is integer
and table2
common column is String
For example,
select a.id as id,a.name as name,b.address as address
from Personal as a,Home as b
where a.id=b.studid
Here a.id
is an integer
while b.stduid
is a string
, but Data of both columns is the same.
How can I get the result of the query using hql query?
Some of the commonly supported clauses in HQL are: HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee . We can also create alias such as from Employee emp or from Employee as emp . HQL Join : HQL supports inner join, left outer join, right outer join and full join.
You may create a calculated column in your entity: @Formula(value = " concat(first_name, ' ', last_name) ") private String fullName; And in your HQL you just refer to this field as you would do to any other.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With