-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I reuse the Java superclass for multiple subclasses?
PostPosted: Thu Jun 08, 2006 5:38 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 10:53 am
Posts: 24
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.3

Hi, I mapped a legacy table with table-per-class-hierarchy strategy. All the subclasses only differs from eeach other by the discriminator, which means all the properties are the same as the super class. To match it this way is because all the required queries are all directly to subclasses, use inheritance mapping saves me a lot of restriction passing from higher layer in the application, and it did improve some performance. Now my trouble is I have to physically create the Java class for each subclass, even it only just extends the super class with empty body. Is there a way to save this Java class creation? I tried to use the same name attribute for each subclass with superclass name, and use entity-name to distinguish each subclass, but I got a WrongClassException at runtime, Hibernate complains that when populating ids for the subclasses.

Any help? Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 12:19 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're using union-subclass to map this, then I don't think that you can avoid having a separate class for each mapping. If you use <subclass> with a discriminator and a nested <join>, then you are only specifying one class (the one in the <class> element), so that should work for you. The downside to this is that you need a table for the superclass, so that you can join from it to the "real" tables. The superclass' table would probably contain only the IDs and discriminators, but seeing as they're unique anyway (assuming that you're currently using union-subclass) that would probably be a good thing.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 4:07 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
the key is in the 'entity-name' property in the mapping files.
Hibernate reference: 5.3. Mapping a class more than once

also you will need the session functions with entity-name parameter:
save(String entityName, Object object) instead of save(Object object)
get(String entityName, Serializable id) instead of get(Class clazz, Serializable id)
etc...

also too, you will need to use entity-name in HQL queries


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 9:42 am 
Beginner
Beginner

Joined: Tue May 23, 2006 10:53 am
Posts: 24
pepelnm, tenwit:

Thanks for both of your replies! It turns out using entity-name version of CreateCriteria() solved my problem. My code looks so cool now! Thanks a lot!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.