-->
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.  [ 10 posts ] 
Author Message
 Post subject: many-to-one "class" with getter returning supercla
PostPosted: Thu Jul 15, 2004 8:04 am 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
Hi, I'm wondering if the following behaviour of Hibernate 2.1.4 is intended, and especially if it is going to stay that way in future versions.

Suppose I have the following abstract base class, which is NOT mapped:

Code:
public abstract class AbstractFoo {
  private AbstractFoo parent;

  public AbstractFoo getParent() {
    return parent;
  }

  public void setParent(AbstractFoo parent) {
    this.parent = parent;
  }
}


Now I have the following subclass, which is mapped:

Code:
public class Foo extends AbstractFoo {
  private Long id;
  private String name;

  // getters/setters for "id" and "name" properties

  // no getter/setter for "parent" property here,
  // we use getter/setter of superclass
}


This is the mapping:

Code:
<class name="eg.Foo" table="FOO">
  <id name="id" column="foo_id">
    <generator class="native"/>
  </id>
  <many-to-one name="parent" class="eg.Foo" column="parent_id"/>
  <property name="name"/>
</class>


Now, please note that in the mapping it says that Foo.getParent() will return an instance of Foo, but the actual (inherited) getter returns an instance of AbstractFoo.

In practice, this works quite well and perfectly fits my needs. But I wonder if this behaviour is intended, and if it is going to stay that way in future Hibernate versions.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 8:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually this is not related to Hibernate but to Java. Your constract ask for an AbstractFoo, it will return you an AbstractFoo casted instance.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 9:23 am 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
emmanuel wrote:
Actually this is not related to Hibernate but to Java. Your constract ask for an AbstractFoo, it will return you an AbstractFoo casted instance.


Yes, but the question is, does Hibernate expect (and require) getParent() to return an instance of Foo, as has been specified in the mapping? Or does it just require an Object to be returned, then using reflection to see what it really is? If the latter is the case (and in case this is intended and going to stay), then it should be clearly documented somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 4:05 pm 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
So, can anyone give a definitive answer? I'd really like to know if I can rely on this behaviour.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 9:48 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The only requirement is that the instance set inside obj.setParent(...) has to be of the type declared inside the mapping.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 10:04 am 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
emmanuel wrote:
The only requirement is that the instance set inside obj.setParent(...) has to be of the type declared inside the mapping.


I'm note sure what setParent() has to do with that. In my example above it expects an instance of AbstractFoo, so when Hibernate is trying to invoke it with an instance of Foo, it would work quite perfectly.

I was rather concentrating on getParent()...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 10:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
setParent is the entry point where users can break the Hibernate contract, that's all.
If you were saying setParent(Object) the instance given has to be on the one mapped in you many-to-one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 10:25 am 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
emmanuel wrote:
setParent is the entry point where users can break the Hibernate contract, that's all.


Well, not quite. I could also opt to return something completely different in getParent(), in which case setParent() would be completely irrelevant.

emmanuel wrote:
If you were saying setParent(Object) the instance given has to be on the one mapped in you many-to-one


I hope I understood that correctly - are you saying that whatever contract setParent() defines (in terms of argument type), the argument must be of the type specified in the mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 7:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
blizzy wrote:
I hope I understood that correctly - are you saying that whatever contract setParent() defines (in terms of argument type), the argument must be of the type specified in the mapping?

Yes

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 8:14 am 
Newbie

Joined: Thu Jul 15, 2004 7:43 am
Posts: 16
emmanuel wrote:
Yes


Great! Many thanks for your help :)


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