-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Annotations, inheritance and interfaces
PostPosted: Wed Apr 20, 2005 12:42 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Hi,

I'm using Hibernate Annotations and I'm trying to map the following relationship:

Code:
interface Animal
{
  public List<Animal> getChildren();
}

class Dog extends Animal
{
  public List<Animal> getChildren()
  {
     return puppies;
  }
}


Anyway, as you can see, the problem is that Dog (a concrete class) would like to return a list of Animal (an interface). Ideally I would like to use the following markup:

Code:
@Entity()
interface Animal
{
  public List<Animal> getChildren();
}

@Entity()
class Dog extends Animal
{
  public List<Animal> getChildren()
  {
     return puppies;
  }
}


such that both entities are stored within a single table (Animal) that may contain Dogs or any other animal that might be added in the future. The main problem here is that I'm trying to @Entity an interface. Is this legal? Do you have any suggestions on how to map this? I really can't use abstract classes in this case because in my actual application I have classes which implement multiple interfaces and they cannot reasonably get transformed into a single "extends" statement. Your feedback would be most appreciated.

Thank you,
Gili


Top
 Profile  
 
 Post subject: Re: Annotations, inheritance and interfaces
PostPosted: Wed Apr 20, 2005 1:09 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Maybe the example would make more sense if one replaced getChildren() by getFriends()

Basically Animals have other Animal friends, but these could be Dogs, Cats, Cows, etc... This is pretty basic POJO inheritance. I'm just wondering how one translates this into a database mapping. To the best of my knowledge, the documentation does not discuss interfaces at all. It seems to gloss over this detail of inheritance and focus exclusively on the different mapping methods (one table, joined tables, etc). Maybe there is a good reason for this, but it isn't obvious to me.

"I'm the boss, throw me a bone..." :)

Gili


Top
 Profile  
 
 Post subject: Re: Annotations, inheritance and interfaces
PostPosted: Wed Apr 20, 2005 1:12 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Nevermind, I found http://www.hibernate.org/hib_docs/v3/re ... strategies which clearly discusses interfaces. So as far as I can tell, this *should* be working, but isn't (I'm getting a NullPointerException). I've already filed a bug report against it. Hopefully it'll work once this is fixed.

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 7:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
annotated interfaces are not yet supported by the annotations, sorry.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:09 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
emmanuel wrote:
annotated interfaces are not yet supported by the annotations, sorry.


Ok, I'm going to file a RFE.

The problem is that XDoclet doesn't seem to support annotated interfaces either so I'm left without a workaround.

Any chance you could give me a ballpark figure of when you expect this RFE to get implemented?

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:22 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why not implement it yourself?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:25 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
Why not implement it yourself?


Honestly, if I knew how, I would.

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:41 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
I went back and reread the specification (edr2). All they say is "The entity bean class is a concrete, not an abstract, class.[2]" and "[2] We plan to address the issue of the interpositioning of abstract classes in the entity bean inheritance hierarchy at a later point."

But nowhere in the specification do they seem to talk about mapping inheritance implementing interfaces. What does this mean? Is this topic outside the scope of the standard? Is this supposed to get handled automatically without an explicit @Entity() tag? Is this kind of mapping impossible according to EJB3 but possible according to Hibernate's documentation?

Scary stuff :)

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:42 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It's quite clear: Only concrete classes are supported by this draft, the issue is being worked on. I don't know what other questions there could be...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 9:10 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
It's quite clear: Only concrete classes are supported by this draft, the issue is being worked on. I don't know what other questions there could be...


Is this one of those cases where Hibernate won't implement it until the draft does? or are we considering Hibernate-specific tags like we already have for other cases? I'm not asking for clear-cut dates; just trying to ascertain if you guys are telling me "don't expect this feature for at least another month" or something of that nature.

Christian please try to understand, I am not trying to be pushy here, just trying to schedule this issue into my project. The more I know, the better.

Thank you,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 9:14 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The answer is, to the best of my knowledge, Maybe.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 2:11 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
The answer is, to the best of my knowledge, Maybe.


Ok, so jokes aside, is there a workaround?

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 10:11 am 
Newbie

Joined: Tue Apr 19, 2005 11:24 am
Posts: 8
I have the exact same thing with my model in terms of having an interface that has a function that returns a generic collection of the same type as the interface. I first tried using annotations, and that got me along up until a certain point. That point was the fact that annotations will work, but if your class extends any classes that are Serializable, then the Hibernate parser (?) will try to continue to look for annotations in those classes as well. This is just not realistic because what if you decide to extend a class that you personally did not write? You cannot just add an @Transient tag to anything that you want. I posted a similar topic about this, and Emmanuel told me that this issue will be resolved in the next EJB3 draft (whenever that is). I personally cannot wait that long, so I have resorted to using XML mapping. When it is implemented in the draft, I will go back and change my code simply because if you add one interface / class, you must also add the corresponding XML mapping for it. Having everything in one place just makes refactoring much easier.

As for XDoclet, I can't help you there. I am pretty new to Hibernate, and trying to throw out any suggestions regarding that would be like throwing poo at the wall and see if it sticks.

Anyway, here's how I setup my interface / class structure: (using your "Animal" naming convention)

Code:
public interface Animal extends Serializable {
    Integer getId();
    void setId(Integer value);
    public List<Animal> getChildren();
    public void setChildren(List<Animal> children);
    public Animal getParent();
    public void setParent(Animal parent);
    public void addChild(Animal child); // My function return a "boolean" because I use a Set instead of a List
    ...
}


Quote:
I really can't use abstract classes in this case because in my actual application I have classes which implement multiple interfaces and they cannot reasonably get transformed into a single "extends" statement.


Hopefully there may be a workaround for this. What I did was have an AbstractAnimal that just implements the getId() / setId() functionality, and then have a ChildAnimal and ParentAnimal that implement the rest of the functions above (granted a ChildAnimal cannot have chlidren...in my application I had to logically stop it somewhere).

As for my mapping, it's something of the following:

(Animal.hbm.xml -- Covers both the Animal interface and AbstractAnimal class)
Code:
...
<hibernate-mapping>
    <class name="a.b.c.Animal" table="Animal" lazy="false">
        <id name="id" column="ANIMAL_ID">
            <generator class="native"/>
        </id>

        <joined-subclass name="a.b.c.AbstractAnimal" extends "a.b.c.Animal" lazy="false">
            <key column="ANIMAL_ID"/>
        </joined-subclass>
    </class>
</hibernate-mapping>


(ChildAnimal.hbm.xml)
Code:
...
<joined-subclass name="a.b.c.ChildAnimal" extends="a.b.c.AbstractAnimal" lazy="false">
    <key column="ANIMAL_ID"/>
</joined-subclass>


(ParentAnimal.hbm.xml)
Code:
...
<joined-sublcass name="a.b.c.ParentAnimal" extends="a.b.c.AbstractAnimal" lazy="false">
    <key column="ANIMAL_ID"/>
    <set name="children">
        <key column="parent_id"/>
        <one-to-many class="a.b.c.Animal"/>
    </set>
</joined-subclass>


For my model, a given child can only have one parent, but one parent can have many children. I am not sure if you are doing a many-to-many relationship, but if you are, I can't help you there. My model may be slightly confusing and some parts may be unnecessary for your application, but hopefully this should point you in the right direction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 1:09 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
DyslexicMonkey,

Thanks for your extensive explanations, they were very helpful to me. At least now I know that I am not the only one running into these problems. My current strategy is similar to yours, holding down the fort with preexisting technologies until Annotations is stable.

Emmanuel, it would probably be useful to add this to the "limitations" section of the Annotations release notes so other developers know to avoid using it if they need interface inheritance or inheriting from classes that implement serializable.

All in all, I wanted to thank Emmanuel for doing a good job. See you guys when edr3 comes out :)

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The next draft will adress the abstract class isue, but not inheritance.
I'm thinking at the interface issue, but it's not simple to declare it through annotations.
In hbm, an interface is <subclass>ed by several classes. But I class has only 1 declared superclass or super interface.
This is different in an annotations approach.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.