All,
Our team is working on using Hibernate but using the native SQL calls as opposed to HQL so it is easier on our DBA...
[/qoute]
Why not just show him the log of executed query statements ? ;)
(just kidding ...but then again, it would make your life easier ;)
Quote:
Code:
select {employee}.emp_name as {employee.empName},
{department}.dept_name as (department.deptName}
from employee {employee}, department {department}
where {employee}.eid = :eid
and {employee}.dept_id = {department}.dept_id
1) is this correct?
It looks fine and dandy by me.
Quote:
2) can this be simplified any or are all of the mappings needed even with the mapping we have done in hibernate.cfg.xml
eh - simplified ? The only thing you have done is replacing short names with longer curlier names ... if you don't like the length then just do:
Code:
select {e}.emp_name as {e.empName},
{d}.dept_name as {d.deptName}
from employee {e}, department {d}
where {e}.eid = :eid
and {e}.dept_id = {d}.dept_id
And remember that the {e} and {d}'s meaning is defined by your createSQLQuery call!
String aliases[] = new String[] {"e", "d"};
Class classes[] = new Class[] { Employee.class, Department.class };
createSQLQuery(sqlstr, aliases, classes);
Quote:
3) does Hibernate translate this straight to SQL or does it actually go from this to HQL then to SQL?
[/qoute]
It goes almost straight to SQL (it IS SQL!) - the only thing hibernate does is a simple string replace from the alias names and the property names to their mapped table names and property names! It is REALLY REALLY simple - nothing fancy ;)
You could even write it without the alias names - but then you would have to know which column name Hibernate would expect for eg. dept_name (it would probably be something like "dept_name0_"
Quote:
4) would this work:
Code:
select {employee}.emp_name, {department}.dept_name
from employee {employee}, department {department}
where {employee}.eid = :eid
and {employee}.dept_id = {department}.dept_id
Probably not since you are not using the expected column names by hibernate. (note: the above is actually the ideal way to have createSQLQuery(), but due to the need to limit the columnnames and making them unique Hibernate does some processing of these to ensure the uniquenes - createSQLQuery() has to live by these rules)
Quote:
5) Can we use anything as the table alias or is it bound to the object name by reflection?
There are NO reflection used in this!
You can use anything you want as table aliases - we just provide you with a HQL version of the "table" name to make it easier to write the sql....
Quote:
6) the examples I have seen of this are both on the roadmap, are there other examples available
As always - look in the unit test and search for createSQLQuery
Quote:
sorry if I beat this to death.
I'm actually really surprised that people don't get the simplicity and no-magic there is about createSQLQuery() - most people think we are doing something really hardcore processing or manipulation of the string put into createSQLQuery....we DON'T!, ok ? ;)