• Post Reply Bookmark Topic Watch Topic
  • New Topic

Criteria across multiple tables  RSS feed

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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(Restriction.ge("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?


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!