-->
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: one-to-many mapping to an interface
PostPosted: Fri Sep 16, 2005 8:54 am 
Newbie

Joined: Fri Sep 16, 2005 8:05 am
Posts: 7
Hibernate version: 3.0.5

Hi,

I am having difficulty working out the correct mapping to use for a one-to-many relationship to an interface. I'm sure that this question has been asked before, but after doing a fair bit of reading and searching on the forums I have yet to find a solution.

Consider the following class hierarchy (which is a subset of my actual class hierarchy):

Code:
public abstract class Persistent{
    private Long id;
    private int version;
    ...
}

public interface FormulaInput{
    public double getInputValue();
}

public class Constant extends Persistent implements FormulaInput{
...
}

public class Entry extends Persistent{
...
}

public class NumericEntry extends Entry implements FormulaInput{
...
}

public class Formula extends Persistent implements FormulaInput{
    private Set<FormulaInput> inputs = new HashSet<FormulaInput>();
    ...
}


When mapping this kind of structure I have been using <joined-subclass> to do table per subclass mapping. I have not mapped the interface, so I have tables t_persistents, t_constants, t_entrys, t_numeric_entrys and t_formulas. I've been using XDoclet2 to generate the mapping files, and Hibernate Tools to generate the database schema.

Now my problem is that I've so far not been able to work out how to map the input property of the Formula class. Ordinarily, I would just use a <one-to-many> inside a <set>. However, as FormulaInput is not mapped I cannot use it as the value of the class attribute in the <one-to-many>. Even if I did map the FormulaInput interface to a table, the Constant and NumericEntry classes would then have to be joined subclasses of two classes, which I don't think can be done.

Has anyone got any ideas as to how I could map this class hierarchy?

Thanks in advance,

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 9:06 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
One thing you could do is introduce another class that is the superclass of all of the persistent objects that implement the interface.

What you have there isn't really a hierarchy because of the interface. Hibernate has to know how to create a class from a row, and a row from a class. It can't do that if it doesn't have a discriminator and a hierarchy to work with.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 11:13 am 
Newbie

Joined: Fri Sep 16, 2005 8:05 am
Posts: 7
Hi,

Thanks for your reply. Unfortunately the structure of my class hierarchy is such that the only way I could introduce a superclass for all classes that implement the interface would be by multiple inheritance, which we all know is not possible in Java.

I guess what I need is a "one-to-many-any" relationship...!

cheers, Rob


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 1:30 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Yes, a one-to-many-any is the way to go if you have this kind of design. We don't have multiple inheritance in Java (thankfully) but we do have encapsulation, and I think that's what you want to use to hide the type of the private field.

IMHO, your class hierarchy proabaly needs some looking at if you have to use one-to-many-any. If you are implementing the same interface with a bunch of different, supposedly unrelated classes, then those classes could probably be related in some way. But hey... that's just my opinion, FWIW. :)


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.