-->
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: non-persistent child subclass object value, in parent table
PostPosted: Fri Aug 03, 2007 4:58 pm 
Newbie

Joined: Fri Aug 03, 2007 4:44 pm
Posts: 6
using NHibernate v1.2 with .NET 2.0 in C#...

I have a parent object: Fault, i have implemented a State Pattern for the child object: FaultStatus. FaultStatus is an Abstract Base Class.

the parent object Fault has a FaultStatus property:

public class Fault
{

private FaultStatus _faultStatus;

public FaultStatus FaultStatus{
{
get { return _faultStatus; }
}

}

Since FaultStatus is ABC, i have several subclasses. Each subclass has a specific ID associated with it. There is a single FaultStatus table in my database, and the Fault table has a FaultStatusId column, which is the relationship to FaultStatus table.

public class Creating: FaultStatus
Id = 1

public class InProcess: FaultStatus
Id = 2

public class Closed: FaultStatus
Id = 3


...


how do I map Fault.FaultStatus to the Fault table FaultStatusId column?

I have tried using "component" in the Fault.hbm.xml mapping and have mapped the Id property of FaultStatus to the FaultStatusId column. This works for saving Fault objects to the database.

The problem occurs when I try to load a Fault from the database. I need NHibernate to know which subclass to load for the Fault.FaultStatus property, based on the FaultStatusId column in the Fault table.

... as a side note - the values in FaultStatus table will never change and I will never add / remove records from that table through nhibernate. If i ever had to add / remove FaultStatus records, I will do so manually in the database and create a new subclass of FaultStatus class and manually set the Id of the new subclass, based on the id value assigned in the database. ... with this in mind, I am assuming that FaultStatus should not be a persistable entity because I do not want NHibernate to persis a copy of the FaultStatus to the FaultStatus table, I am only using the FaultStatus table for referential integrity within the database... consider FaultStatus class could easily be an Enum instead, but I have implemented it as a State pattern, so it is a set of classes instead.

...

long story short: i need help mapping Fault.FaultStatus from my entity into my table, and back into my entity with the correct FaultStatus subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 5:12 pm 
Newbie

Joined: Fri Aug 03, 2007 4:44 pm
Posts: 6
blech.... looks like i'm out of luck:

http://jira.nhibernate.org/browse/NH-943

this is the same scenario of mapping that i need.

can someone else verify if i'm out of luck or show me how to make it work? i'm not opposed to having FaultStatus be a "persistent" object, provided that it never actually writes data to this table.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 6:32 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If you're trying to implement the State Pattern you might just tie the numeric value of your state to a field rather than a property and then have the property getter handle the instantiation and return of the transient FaultStatus object based on the value of the field.

If you're planning on supporting state changes from the state object then at the time when you call a method on the state object to change the state it would delegate the change to a method on the parent object which could update the numeric state field. The parent object would then be responsible for persisting the state value (as it probably does now).

Not sure if that fits what you're trying to accomplish, but at least I can say I took a shot! :)

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 12:11 pm 
Newbie

Joined: Fri Aug 03, 2007 4:44 pm
Posts: 6
the same idea ran through my head yesterday... technically, it would work. i just don't like it because it violates a few of the principals that i'm trying to hold myself and my dev team, to. but... i'll probably end up doing that, if i can't find an alternate solution by monday morning.

the good news is, at least someone else is validating that my problem is real, and a solution that ran through my head was suggested by that person. so it's not just a case of me being completely stupid, again. :)

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 3:36 pm 
Newbie

Joined: Fri Aug 03, 2007 4:44 pm
Posts: 6
ok... so maybe i am just being an idiot again.

i already have a FaultStatus table for referential integrity... and i had tried using a FaultStatus.hbm.xml to map that table with the needed subclasses... but i had the "id" mapped incorrectly - i set the generator to "assigned" and it works.

no need for me to subclass a component. i just had to map the subclass of the FaultStatus property, correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 4:58 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
So how have you handled your FaultStatus subclasses? Did you you table-per-class-hierarchy and use the id as the discriminator too? I'd be interested to see your mappings...

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 6:43 pm 
Newbie

Joined: Fri Aug 03, 2007 4:44 pm
Posts: 6
i used table per class hierarchy, storing only the ID and "StatusType" (name of the status) of the FaultStatus object.

it's nothing more than the standard subclass capabilities of nhibernate, combined with the "assigned" id type, to allow my code the ability to do the assignment of the id.

we used the StatusType as the descriminator, initially... i may change it over to the Id instead, though.

here's my Fault object (parent) mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping default-access="field.camelcase-underscore" default-lazy="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="Namespace.Fault, Assembly" table="Faults">
<id name="Id" type="integer">
<generator class="native" />
</id>
<property name="prop1" />
<property name="etc" />

<many-to-one name="FaultStatus" class="Namespace.FaultStatus, Namespace" column="FaultStatusId"/>
</class>
</hibernate-mapping>


...

and here's my FaultStatus (abstract sub-classed child) mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping default-access="field.camelcase-underscore" default-lazy="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="NameSpace.FaultStatus, Assembly" table="FaultStatus">
<id name="Id" type="integer">
<generator class="assigned" />
</id>

<discriminator column="StatusType" type="String"/>


<subclass name="Namespace.Creating, Assembly" discriminator-value="Creating" />

<subclass name="Namespace.InProcess, Assembly" discriminator-value="InProcess" />

<subclass name="Namespace.Closed, Assembly" discriminator-value="Closed" />
</class>
</hibernate-mapping>

...

the key to all of this is that each of my subclasses has the Id and StatusType values hard coded, and i have the Id property mapped as "assigned". Then, I have a "FaultStatus" table that contains the Id and StatusType columns, for referential integrity, allowing NHibernate to decide what to do with the FaultStatus property/object...


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.