-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping two tables that have same database schema
PostPosted: Mon Jul 20, 2009 7:27 am 
Newbie

Joined: Mon Jul 20, 2009 7:11 am
Posts: 6
I am working with a legacy system and am trying to avoid changing the database schema.

This system has two database tables, SurveyChoices and ChoiceGroupItems, which have the same basic schema, except that SurveyChoices contains a foreign key which references a table named SurveyQuestions, while ChoiceGroupItems contains a foreign key which references a table named ChoiceGroup.

I can make this work by declaring Java classes named SurveyChoice and ChoiceGroupItem, and explicitly mapping them to their respective tables and columns. I am wondering if it is somehow possible to not have to declare the member variables in each class and write getters and setters.

I tried Java inheritance - I wrote a class named AbstractChoice and make both SurveyChoice and ChoiceGroupItem extend it. I'm using annotations for my mappings, so I annotated all of the properties in AbstractChoice. I then discovered that annotations in Java aren't inherited, so this didn't work.

I've thought of using an external entity in the XML mapping file to "include" the common definitions in both XML mapping files, but am wondering if there is an easier solution.

I'm relatively new to Hibernate, so maybe there's a way to do this. I think what I'm looking to do is "table per concrete class" without duplicate property/mapping information. Note that these two classes are not ever used polymorphically.

Thanks for any suggestions you might have.

Ryan


Top
 Profile  
 
 Post subject: Re: Mapping two tables that have same database schema
PostPosted: Tue Jul 21, 2009 7:18 am 
Newbie

Joined: Mon Jul 20, 2009 7:11 am
Posts: 6
In case anyone out there is trying to do the same thing I was, I got this to work using external entities in an XML mapping file. This was suggested in the post at viewtopic.php?f=1&t=930154.

I wrote an XML fragment declaring the common properties in a file called AbstractChoice.hbm.xml. It looks like this:
Code:
<id column="CID" name="cid">
  <generator class="native"></generator>
</id>
<property name="choiceNumber"/>
<property name="value" column="ChoiceValue"></property>
<property name="choiceText"/>
<property name="shortText"/>
...


Then, in the two XML files describing the two classes have the properties above in common, I have the following DOCTYPE declaration:
Code:
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
[ <!ENTITY common SYSTEM "AbstractChoice.hbm.xml"> ]   
>

and then a reference to the external entity common as part of the class element:
Code:
<class name="Choice" table="SurveyChoices">
  &common;
  <many-to-one name="survey" column="SurveyID" class="Survey"/>
</class>

Note that the "survey" column is only appropriate for Choice and not the other class that uses the common schema, which is why it is here, rather than in AbstractChoice.hbm.xml.

The hibernate.cfg.xml file referenes only Choice.hbm.xml and ChoiceGroupItem.hbm.xml, not AbstractChoice.hbm.xml.

Finally, there is an abstract Java class named AbstractChoice containing the common properties, and then subclasses that extend AbstractChoice for Choice and ChoiceGroupItem.

I'd still like to do this using annotations, so if anyone has any ideas on how to do that I'd love to hear them.


Top
 Profile  
 
 Post subject: Re: Mapping two tables that have same database schema
PostPosted: Thu Jul 23, 2009 10:43 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
answer this guy his question. :D he answered mine, he deserves it. ;P


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