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: Polymorphism question (please help)
PostPosted: Sun Dec 10, 2006 1:44 pm 
Newbie

Joined: Fri Sep 15, 2006 6:07 am
Posts: 5
Hi all,
I am using Hibernate 3.1 and I am facing the following problem, somehow I have the feeling that I am missing something and the solution is quite simple :-)

I have two classes, Device:
Code:
public class Device  implements Target
{
    public String ip;
    public String hostName;
    ....
}

and Company:
Code:
public class Company  implements Target
{
    public String name;
    public String version;
    ....
}
These classes are successfully mapped to DEVICE_T and COMPANY_T respectively.
In addition I have a class that associated with Target (i.e.Device or Company) as follow:
Code:
public class Task
{
    public Target target;
}

My Question is how can I map the Task class?

Thanks in advance,
Guy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 7:21 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
you need 2 coulmns in the table mapped to Task class

1. Descriminator coulmn which tell it what kind of target to get
2. foriegn key coulmn

and you need to map target many-to-one using the any mapping

check this out for more info
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-types-anymapping

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 12:31 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Is there any way to map this reference using multiple database columns?

The discriminator column + "any" mapping means we would have to give up referential integrity on that column.

When we have a small number of possible subclasses of the interface, we'd like to map the association with two (or three) explicit foreign key relationships (only one of which would ever be set at any point in time for a given row).

At the moment (in terms of the example given by Guy) we are achieving this by explicitly mapping Company and Device properties on the Task class (which are not exposed to the business logic of the application), then we use a bunch of messy and potentially error-prone if/else code to set/get return a single Target instance depending on which type of explicit property has been/is being set.

If there's no mapping in Hibernate that could achieve this, would the dev team be willing to accept a patch that did this?

_________________
Cheers,
Shorn.


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.