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.  [ 4 posts ] 
Author Message
 Post subject: Upcasting to Base Class before performing Session.Save()
PostPosted: Wed Apr 23, 2008 4:23 am 
Newbie

Joined: Wed Apr 23, 2008 4:09 am
Posts: 2
Hi,

I've written a small proof-of-concept app for us using NHibernate in a new project, but there's one issue I can't resolve.

The test model is that we have an Employee base class, extended by others such as Engineer, Director, Cleaner etc.

For various reasons, we only want to persist the Employee class (with any other role-specific detail being stored in an xml field in Employee).

So, we have the following:
- SQL Server 2005 table "Employee"
- Employee.hbm.xml mapping file
- Employee Class and additional aforementioned sub-classes.

The Session.Save() method is in the Employee Class.

When we try to persist an instance of Employee, it works perfectly, as expected.

However, if we want to persist a Director, we get the message:

"MappingException was unhandled"
"Unknown entity class: testapp1.Director"

Which sort of makes sense, because we haven't written a mapping file for the Director, just the Employee. But we just want to persist it as an Employee.

We tried doing .Save( (Employee)director) but still get the same exception.

Can anyone explain how this should be done?

Any help much appreciated

Artie


Top
 Profile  
 
 Post subject: Upcasting to Base Class before performing Session.Save()
PostPosted: Wed Apr 23, 2008 5:09 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Artie,

I think you'll need to add a subclass element to your mapping, just without any extra properties for the subclass.

Regards,
Richard


Top
 Profile  
 
 Post subject: Re: Upcasting to Base Class before performing Session.Save()
PostPosted: Wed Apr 23, 2008 5:24 am 
Newbie

Joined: Wed Apr 23, 2008 4:09 am
Posts: 2
FlukeFan wrote:
Hi Artie,

I think you'll need to add a subclass element to your mapping, just without any extra properties for the subclass.

Regards,
Richard


Thanks for your response, Richard.

One of the reasons we had used the single Employee class for persistance was so that we only had to configure that class in the database and hbm file.

We wanted to be able to hide the fact that the other Employee sub-classes would also be getting persisted VIA the Employee persistance, and didn't want to refer to each of these classes in the hbm file.

Is there another way of doing this?

Artie


Top
 Profile  
 
 Post subject: Re: Upcasting to Base Class before performing Session.Save()
PostPosted: Wed Apr 23, 2008 5:37 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Artie,

Quote:
didn't want to refer to each of these classes in the hbm file


If NHibernate isn't told about the extra classes then I don't think it can create them or persist them (understandably).

What are the concerns with referring to the sub-classes in the mapping file?

Other than that, you could try removing the subclassing, and replace it with a state/strategy pattern, but it sounds like more work than simply specifying the classes in the mapping.

Regards,
Richard


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