-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why no embedded subclasses of entities?
PostPosted: Fri Nov 04, 2005 9:43 pm 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
Hibernate 3.1rc2, Annotations 3.1b6. I want to do something like this:

Code:
@Entity
@Table(name="funds")
@Inheritance(strategy=JOINED, discriminatorType=INTEGER)
public abstract class Fund {
   int id;
   int balance;
   ...
}

@Entity
@Inheritance(discriminatorValue="1")
public class CashFund extends Fund {
   // no additional properties for this subclass
}

@EmbeddedSuperclass
public abstract class FundWithBillingAddress extends Fund {
   @ManyToOne
   @JoinColumn(name="billing_address_id")
   BillingAddress billingAddress;
}

@Entity
@Inheritance(discriminatorValue="2")
@Table(name="funds_cc")
public class CreditCardFund extends FundWithBillingAddress {
   String cardNumber;
   ...
}


That is, I have an abstract subclass of an entity that contains some properties that are common to a few different concrete grandchildren of the entity. Those properties are in the grandchildren's joined tables, not in the top-level entity's table.

I can do this using XML mapping files by declaring the billingAddress property in the CreditCardFund class's mapping; Hibernate doesn't seem to care that the property actually came from a superclass.

When I try to do it using annotations, I get "@EmbeddedSuperclass cannot be a subclass of @Entity". Why is that restriction there? This seems like a perfectly legitimate thing to want to do (and you can, just not with annotations!)

In another thread on this forum, one of the Hibernate team said, "You don't need @EmbeddedSuperclass in the middle of the hierarchy, just use @Entity instead" but that doesn't apply here -- in this case there is no separate table for the middle class, it's just where I define common properties of the grandchild classes. I tried using @Entity and got an error from my database about the fund_with_billing_address table not existing.

Thanks!


Top
 Profile  
 
 Post subject: Oops, mistype...
PostPosted: Fri Nov 04, 2005 10:21 pm 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
Bah! Please read "EmbeddedSuperclass" as "EmbeddableSuperclass" throughout my message -- sorry if that confused anyone.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 06, 2005 10:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
if you don't want to have an intermediate table, why do you need an intermediate class?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 06, 2005 1:45 pm 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
Why an intermediate class: (a) So that I can do

Code:
public void writeLetter(FundWithBillingAddress fund) {
   ...
   BillingAddress addr = fund.getBillingAddress();
   ...
}


And (b) because not using one would be bad object model design, in that it would violate the "don't repeat yourself" principle -- I'd end up declaring the property and its column mapping separately in each subclass, of which there are several.

I could get (a) by using an interface, but that wouldn't help with (b).

Why not another table: Mostly because it'd be lousy database design -- there is always a billing address for each of those concrete subclasses, so there's no normalization principle that says the billing address ID should be moved to a separate table. Also, though much less important, every extra table you throw into a query is a performance hit (albeit a small one).

And finally: Because I can do this with the mapping files, and in my opinion it'd be a pity if annotations didn't provide the full expressive power of the mapping files. If they don't, then people are going to have to learn both annotations *and* the mapping file syntax in the future, which kind of defeats a lot of the purpose of going to annotations in the first place.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 6:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Open a JIRA issue, I'll consider this feature... faster if you provide a patch :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 4:35 pm 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
I've filed JIRA issue ANN-147 on this.


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