-->
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.  [ 7 posts ] 
Author Message
 Post subject: mapping: two object types in one set - to two tables
PostPosted: Wed Sep 07, 2005 6:11 am 
Newbie

Joined: Wed Sep 07, 2005 6:01 am
Posts: 4
I have a set with two different kinds of objects (different classes, inherit the same class though).

How do you map this set into two different tables? (each object type has its own table)

Frank


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 11:05 am 
Beginner
Beginner

Joined: Wed Jul 13, 2005 2:18 pm
Posts: 44
Sounds like you have a 'table-per-concrete class' situation. Refer to sections 10.1.5 and 10.1.6 of the hibernate user manual for more information.

_________________
- Matt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 3:52 am 
Newbie

Joined: Wed Sep 07, 2005 6:01 am
Posts: 4
It seems I haven't been very clear. I already store my objects correctly, but need to change something to them when they're in a group. This is my group mapping file, which has to be changed:

Code:
   <hibernate-mapping>
    <class name="project.beer.group.Group" table="groups" lazy="false">
        <id name="id" column="group_id">
          <generator class="increment"/>
       </id>
<set name="grolsches" table="grolsches">
           <key column="gr_group_id"/>
            <one-to-many class="project.beer.grolsch.Grolsch"/>   
       </set>
      <set name="heinekens" table="heinekens">
           <key column="he_group_id"/>
            <one-to-many class="project.beer.heineken.Heineken"/>   
       </set>       </class>

</hibernate-mapping> 


As you can see I have a java class "Group" with a set of Grolsch objects and a set of Heineken objects.

I want them in one set with "Beer" objects (they extend the Beer class), but in that case I will have to seperate them in the mapping file I guess? (because they change something in different tables) How can I do so?

(I do not prefer the workaround : making a "groupId" in the Group.java)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 3:54 am 
Newbie

Joined: Wed Sep 07, 2005 6:01 am
Posts: 4
(I do not prefer the workaround : making a "groupId" in the Beer, or Heineken+Grolsch class)

I mistyped.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 5:23 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Do the beers have a common super-class? If so, then you can change the mapping to something like the following:

Code:
<hibernate-mapping>
   <class name="project.beer.group.Group" table="groups" lazy="false">
      <id name="id" column="group_id">
         <generator class="increment"/>
      </id>
      <set name="beers">
         <key column="gr_group_id"/>
         <one-to-many class="project.beer.BeerSuperClass"/>
      </set>
   </class>
</hibernate-mapping>


You may find the following section of the documentation useful: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#inheritance

Looking at your current mapping files, the Table per class hierarchy looks like it will fit well.

Also, you don't need to specify a table in your set mappings if the association is a one-to-many, as it will be placing a foreign-key in the table of the associated class automatically. The table attribute is only necessary with many-to-many mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 5:27 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Seems there have been a few replies since I first opened the post, so matt_mcgill is correct in his suggestion.

You just need to map your Beer super-class, along with its sub-classes correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 9:51 am 
Newbie

Joined: Wed Sep 07, 2005 6:01 am
Posts: 4
Yes, I changed my "Table per concrete class, using implicit polymorphism" to "Table per class hierarchy" and it works fine. Thanx!


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