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.  [ 7 posts ] 
Author Message
 Post subject: How to map to abstract base class?
PostPosted: Fri Jul 13, 2007 11:15 am 
Beginner
Beginner

Joined: Thu May 31, 2007 1:19 pm
Posts: 23
Hi,

A set of classes that inherit from an abstract base class that has a set of common properties. Each concrete class has it's own table in the DB.

abstract class AC{
public string Id,
public string Name,
public string Description
}

class C1 : AC{
public int Credit
}

class C2 : AC{
public int Age
}

In my mapping XML, I would like not to have to define the Id, Name and Description _for each_ <class> elment (i.e. I wish to have a similar notion of inherited values in the XML definition).

Is this possible?

Thanks,
Mark.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 2:09 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I don't think so.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 4:50 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
[... oops - I misread your posting ...]
No, I also think it's impossible. Maybe the attributes in the the contributions can do this - but I did not look ...
(We use our own code generator to create the XMLs, and there, you can do such things easily).
Regards
Harald


Top
 Profile  
 
 Post subject: Yes, attributes help
PostPosted: Tue Jul 17, 2007 3:24 pm 
Beginner
Beginner

Joined: Thu May 31, 2007 1:19 pm
Posts: 23
Hi,

One can do something like this:

[Class(NameType(Class1))]
Class1: BaseClass{
}
[Class(NameType(Class1Ext))]
Class1Ext: BaseClass{
[Property]
}
BaseClass{
[Id, Generator]
[Property]
[Property]
}

As long as there is no "Class" attribute for the base class, the attributes are inherited together with the properties of the class (actually if you can examine the XML configuration created by the HbmSerializer, and it expands the super classes out, copying the attributes from the base)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 7:39 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
You may want to check out my blog posting on this at
http://blogs.chayachronicles.com/sonofnun/archive/2007/06/26/251.aspx

You CAN do what you are after.

Hope this helps,
Mike

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject: Yes but...
PostPosted: Wed Jul 18, 2007 5:24 am 
Beginner
Beginner

Joined: Thu May 31, 2007 1:19 pm
Posts: 23
I looked at this in my travels... I think the problem is with these subclasses is that one needs a descriminator column - i.e. if I understand it, a column that tells NHibernate which subclass to instantiate in a table-per-class hierarchy scenario.

Now, this is not something I have in my schema :(

Mark.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 10:28 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Yes a discriminator column is required for the table-per-class-hierarchy strategy and if you have no control over your db schema then that wouldn't work.
However, if you ARE actually able to modify db schema and are holding to a predefined schema based on your relational view of the domain, then I'd encourage you to consider putting the discriminator in. Domain design should determine the table schema, not vice versa.

Good luck!
Mike

_________________
If this helped...please remember to rate it!


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