-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: InheritanceType.TABLE_PER_CLASS and split/archive tables
PostPosted: Tue Feb 06, 2007 6:24 pm 
Newbie

Joined: Tue Feb 06, 2007 6:08 pm
Posts: 2
Hibernate version: 3.2.2

I have an existing schema with items split into two identical tables - 'active_tasks' and 'completed_tasks'. Upon completion of a task it is moved from the active_tasks table to the completed_tasks table.

What is the best way to map a Task object (or hierarchy) onto these two tables?

I finally got InheritanceType.TABLE_PER_CLASS to work with an abstract Task class and two subclasses, but the SQL generated is very poorly performing with a moderately large completed_tasks table.

The SQL is basically:

select * from (select * from active_tasks union select * from completed_tasks) where task_id=?

Is there anything I can do to improve this? This has to be a fairly common problem of having identical items stored in multiple tables (say tables for each month, etc.).

Thanks,

--Kevin


Top
 Profile  
 
 Post subject: Using NamedNativeQuery
PostPosted: Tue Feb 06, 2007 7:31 pm 
Newbie

Joined: Tue Feb 06, 2007 6:08 pm
Posts: 2
Well, I may have figured out a way to get around this, but it is not pretty and is fragile should hibernate internals change (change clazz_ to something else), etc.

Code:
@Loader(namedQuery="Task.findByID")
@NamedNativeQueries(value = {
        @NamedNativeQuery(
                resultClass=Task.class,
                name="Task.findByID",
                query="SELECT t.*, 1 as clazz_ FROM active_tasks t " +
                      " WHERE t.task_id = :id " +
                      " UNION " +
                      " SELECT " +
                      "  t.*, 2 as clazz_" +
                      " FROM" +
                      "  completed_tasks t" +
                      " WHERE t.task_id = :id"
        )
})


But, this performs much better for my scenario where the number of tables is small (2), but the quantity of data in them is large.

I would still like to know the "right" way to do this if there is one.

--Kevin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.