-->
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: Support for multiple inheritance or generics?
PostPosted: Fri Sep 28, 2007 10:20 pm 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
I am evaluating NHibernate for use in my current project, and first I must determine the ease with which I can map the domain model. The domain model consists of concrete classes each of which inherits from between zero and two interfaces. The interfaces have associations between each other. I designed the domain model with the assumption that I could map the underlying data source to the interfaces’ properties giving somewhat the effect of multiple inheritance. I want to use NHiberate’s table-per-subclass mapping strategy, but I haven’t figured out the XML syntax for a “joined-subclass” that inherits from multiple interfaces. In terms of the relational model, the subclass’s table would simply have one foreign key per parent interface. Does NHibernate support multiple inheritance in this way? If not then how else could I achieve the same result?

---
If NHibernate does not support multiple inheritance in the way I described above, then my second question is: does it support user-defined parameterized types (generics)? I suppose I could remove multiple inheritance from my domain model as follows. In cases which lead to multiple inheritance, instead of having an interface with multiple implementing concrete classes, change the interface into a parameterized concrete class with a property of the parameter type that represents the former subclasses. That way the link between the former interface and former subclasses is still maintained with the same cardinality, and each of the former subclasses can still inherit from one other interface.

So does NHibernate support user-defined generics to make this approach possible? I think this could be possible on the relational side as follows. Each table corresponding to a type that’s used as a type parameter must include a foreign key pointing to the parameterized type’s table. This foreign key will be null in cases where the instance does not belong to an instance of the parameterized type.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 3:40 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
I don't really understand what you're trying to do from an object oriented point of view. So here's a well-regarded suggestion for designing your domain model "do it exactly as you would if Hibernate didn't exist".

When you say does it support multiple inheritance in this way... I don't know if there's an answer to this, because what you've talked about is not multiple inheritance. Multiple inheritance is not supported in C# and interfaces are implemented.


Last edited by PandaWood on Sat Jul 12, 2008 11:22 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 4:42 am 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
PandaWood wrote:
I don't really understand what you're trying to do from an object oriented point of view. So here's a well-regarded suggestion for designing your domain model "do it exactly as you would if Hibernate didn't exist".

That sounds like generally good advice, but actually I didn’t know anything about Hibernate in particular when designing the domain model. Rather, I just had some vague sense there would be a relational database as the underlying data structure.

PandaWood wrote:
When you say does it support multiple inheritance in this way.. yeah I don't know if there's an answer to this, because what you've talked about is not multiple inheritance. Multiple inheritance is not supported in C# and interfaces are implemented not inherited.

I used the term “multiple inheritance” because some people see implementing interfaces as a scaled-down form of inheritance. I can instead call it “implementing multiple interfaces” if that’s more kosher. But in fact, the effect I am talking about achieving is more than just implementing an interface. I am talking about the data structure being “inherited”, which I think is conceivable in an O/R mapping scenario since the underlying data structure sits outside of the CLR.

PandaWood wrote:
If you don't have subclasses, you cant map subclasses. There seems to be some confusion about what interfaces are here.

Interfaces are not classes, so they don't directly affect mapping at all.

I’m a bit confused by what you say here. In the NHibernate documentation, “Chapter 8: Inheritance Mapping”, the example of table-per-subclass mapping uses exactly that: an interface that’s mapped to a table in the database. And I never said I don’t have subclasses. Quite the contrary, I said my domain model consists of concrete classes that each implement zero to two interfaces.

I hope that’s helped somewhat clarify my question in regards to implementing multiple interfaces. Again, if that’s not possible, then what about user-defined parameterized types?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 5:50 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate 1.2 does not support multiple interface inheritance in the way you described. Using the current SVN trunk version you could probably use <join> element in the class mapping to join the interface tables with the class table.

NHibernate 1.2 doesn't support generic entities either (mainly because of the HQL parser limitations), so the second approach wouldn't work. The generic entities support isn't implemented in the trunk neither.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 9:17 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
HappyNomad wrote:
I’m a bit confused by what you say here. In the NHibernate documentation, “Chapter 8: Inheritance Mapping”, the example of table-per-subclass mapping uses exactly that

Sorry I'm just wrong. I didn't even know you could do it - and I have 2 books which don't mention it. You can take what sergey says and probably ignore me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 6:11 am 
Beginner
Beginner

Joined: Fri Sep 28, 2007 9:50 pm
Posts: 34
Thank you sergey for that helpful information, and thank you PandaWood for challenging me to clarify my question. I’ve determined it is possible to adjust my data model to work with NHibernate by using the Bridge design pattern. This breaks each problem interface into an “Abstraction” and an “Implementor”. After that, the only remaining issue is mapping a field of type “IImplementor” in the Abstraction sealed concrete class, where IImplementor is an interface that’s not explicitly mapped. It seems the XML ‘any’ tag is for addressing just such a need.

The problem interface was a problem for NHibernate because it participated in a many-to-many relationship. And thus it needed to be explicitly mapped and it’s implementing classes also needed to inherit that data-structure from it. That was impossible since those implementing classes were already mapped as subclasses of other interfaces. Now the Abstraction class participates in the many-to-many relation, and the implementing classes derive from IImplementor.

I would like to share an observation I’ve made during this exercise. If the method of “multiple (data-structure) inheritance” that I originally described was to be supported by NHibernate, then the ‘any’ tag (which is far from ideal) wouldn’t be needed – when using the table-per-subclass mapping strategy anyway. It would also allow for a more conceptual sort of modelling. So, depending on how well it meshes with NHibernate’s current architecture, I would like to suggest adding it.


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.