-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best Practice for mapping references to TPC Inherritance
PostPosted: Wed May 30, 2007 12:00 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I have this problem quite often where my objects have polymorphic relationships to legacy tables that don't implement SingleTableInherritance mappings, or Joined Subclass Mappings. So say for example I have something like:

@Entity
public class Deployment {
private Target target;

}

@Entity
public class Webserver implements Target {
}

@Entity
public class Desktop implements Target {
}

My two legacy tables (WebServer and Desktop) are really mapped as Table Per Class inherritance mapping strategy because this is a legacy DB. But, my Deployment table has a polymorphic reference to them. I can't modify those tables to conform to STI or JoinedSubclass so what should I do?

I'm contemplating implementing a ComplexUserType mapping that would use an ID and discriminator field to figure out which one I should use. This is so nice in ActiveRecord and it doesn't require that I modify my tables that the reference is pointing at. I started to implement it, and hit a wall because while I can do all the mapping onto my Deployment table. I could tell hibernate to automatically join to the two legacy tables. Is there a way to implement this as a pluggable type? Is there another pluggable interface I can use to do this?

If not what does hibernate provide other than STI and JoinedSubclass for representing polymorphic relationships?

BTW I'm using annotations so if you understand annotations please use those in your examples.

Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 3:21 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Ok so I'll answer my own question.

The answer is mapping the ManyToOne with an any in XML configuration. After I went I home I reread Hibernate in Action, and discovered the exact discussion I needed in there.

However, there is no equivalent to <any> element in annotations. However, it's in the works to add a @ManyToAny annotation as well as a @Any annotation in v 3.3.1. I have a work around which I think will perform better than the discriminator, type that ManyToAny relationship would use, but not as clean.

However, this is probably the weakest part of ORM tools while hibernate allows you to do this. It doesn't recommend it which is bogus because no matter how well you organize your mapping. That doesn't guarentee that there will only one interface through which polymorphism is provided. This isn't some silly work around for legacy issues. You have the exact same issue that TPC produces when you add interfaces. The STI and Joined subclass mapping strategy only allows ONE polymorphic base. However, Java objects can have any number of interfaces. But, these prescribed mapping strategies only provide ONE to us.

I don't see this issue is completely contained to hibernate per se. I think Ruby's ActiveRecord essentially does the same thing, however it's much easier to use, but not as optimized. It's really a problem with what relational DB's provide us. There's just no easy way to solve it perfectly given the relational DB toolset which is absolutely ridiculous given 80%+ of projects are using OO languages. I wonder if we could extend relational DBs to provide a better facility for this.


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