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: How to map a polymorphic collection?
PostPosted: Wed Jan 24, 2007 5:00 am 
Beginner
Beginner

Joined: Mon Apr 24, 2006 10:42 am
Posts: 23
Hi all,

We have a mapping problem:

In our domain model, we have a Container that has a set of Entities (i.e. it is a one-to-many association). Entity is an interface, and the concrete implementation classes are each mapped to a different database table. This is what "Hibernate in Action" refers to as - "table-per-class" model. (It means that each implementation class has a separate hbm.xml file, as opposed to creating a single hbm.xml for the super-class/interface and using a discriminator column...).

Here is the relevant code:

Code:

public class Container
{
    private Set<Entity> entities;
}

public interface Entity
{
    Container getParent();

    void setParent(Container container);
}


public class User implements Entity
public class Device implements Entity




The problem, is how to map a one-to-many, when the "many" is polymorphic, and in separate tables. (i.e. how to map the above Container class)

We have already encountered a similar problem, but with the polymorphism being in the "one" side of the many-to-one association. We were able to map it using the Hibernate "any" mapping element. Unfortunately, "any" is not available for a "set" mapping-element.

We would appreciate any insights.

Thanks a lot,
Naaman


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 10:38 am 
Newbie

Joined: Mon Feb 20, 2006 6:31 am
Posts: 17
I've encountered something similar but with the polymorphism at the one side. You need to define
Code:
<set name="entities"
inverse="true">
    <key column="CONTAINER_ID"/>
    <one-to-many class="Entity">
</set>
in the mapping definition of Container.

On the other side you define an abstract class (maybe it works with interface, otherwise define your interface as abstract class).
The mapping for this class is something like:
Code:
<class name="Entity" abstract="true">
    <id ...>
    <many-to-one name="parent" column="CONTAINER_ID" class="Container"/>

    <union-subclass name="User" table="UserTable">
    ...
    </union-subclass>
    <union-subclass name="Device" table="DeviceTable">
    ...
    </union-subclass>
</class>


As I said before, I've used it in the opposite many-to-one direction , but the concept should be the same...


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.