cbredesen wrote:
Quote:
However I could not do the same for EmployeeDepartment class. Difference between these two classes is Employee class maps to exactly one table in database (so uses @Table annotation) but EmployeeDepartment class is populated by a native custom query.
It sounds like you're confusing mapping with querying. The optional @Table annotation tells Hibernate which table your entity maps to. If you omit it, the ONE table name will be the same as the unqualified Java class name. If you want to map across more than one table, check this section of the manual:
http://www.hibernate.org/hib_docs/v3/re ... ation-joinThe native query you've declared does not map your entity to more than one table; it simply gives a named query that you can invoke using the Query API which is not to be confused with the Criteria API.
If you want optional bind parameters you'll have to create two named queries. Using a DAO, you can abstract this away from the calling program.
Hope this clears things up a bit.
-Chris
Thanks Chris. I understand that named query is different than the hibernate mapping. However, I see the workaround of my problem.
I am thinking of creating a view and map the Java class to a view instead of mapping it to a table. That way I can create a criteria object, add any expression to that criteria and avoid the need to write multiple named queries and conditionally use them.
But what I was under the impression that if I can achieve mapping a Java class to a query using a view, there should be similar mechanism in hibernate without using a view as well. Unfortunately, this seems to be not the case.