File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Criteria across multiple tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Criteria across multiple tables" Watch "Criteria across multiple tables" New topic

Criteria across multiple tables

David Zazeky

Joined: Nov 05, 2008
Posts: 7

I have a legacy database with two tables: Logs and ArchivedLogs. They have identical schemas (LogID, Message, TimePosted, etc...). I have a separate HBM mapping file and Java class for both tables. I'm trying to use Criteria to search both tables and return a single list of results, but I'm having trouble getting the correct syntax. Here is what I tried so far:

Criteria theSearch = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Logs.class);
theSearch.add("TimePosted", timeValue));
return theSearch.list();

The problem with the above code is that it only searches the Logs table. I could separately run the criteria on ArchivedLogs, but the results wouldn't be sorted properly between the two tables.

So I have two questions:

1. Is there any way in Hibernate to map two tables with identical schemas to the same POJO object?

2. If not, can I use a Criteria query to return results from both POJO objects (one per table)? If so, how? Would using plain HQL be a better option?


I agree. Here's the link:
subject: Criteria across multiple tables
It's not a secret anymore!