-->
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.  [ 3 posts ] 
Author Message
 Post subject: Class Hierarchy / Convert One Subclass to Another
PostPosted: Thu Jul 19, 2007 5:36 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
Let's say I have the following class hierarchy...

Code:
public class Field
{
    private int _id;
    private sring _name;

    public int ID
    {
        get {return _id;}
        set {_id = value;}
    }

    public string Name
    {
        get {return _name;}
        set {_name = value;}
    }

    public Field() {}
}

public class TextField : Field
{
    private int _maxLength;

    public int MaxLength
    {
        get {return _maxLength;}
        set {_maxLength = value;}
    }

    public TextField() : base() {}
}

public class DropDownField : Field
{
    private string _items;

    public string Items
    {
        get {return _items;}
        set {_items= value;}
    }

    public DropDownField() : base() {}
}


Basically, I have two classes (TextField and DropDownField) that inherit from Field. Let's also assume I am persisting these using a DB-generated ID and using a single table per class hierarchy mapping with a discriminator column (FieldType).

We have a GUI editor that allows a user to add, edit, and delete text fields or drop down list fields. But now, they want to be able to select a text field for example, and change the text field to a drop down list field. Well, the text field is used in many different places in the application (the ID is used to record data entered for a field for example), and we must keep the ID intact.

Is there a way to "convert" one field type to another field type, while maintaining the ID of the original field and making sure the changes are persisted to the same field?

Example...

Code:
TextField myField = Repository.GetByID(id);
if (typeHasChanged) // Maybe user chose different field type from a drop-down list.
    // Convert myField, a TextField, to a DropDownField???
Repository.Save(myField);


The result would be that myField is now of the type DropDownField and the discriminator column for the field would now reflect that change.

I have shown a simplified version. We basically have about six different field types that all inherit from a base Field class. We need to be able to allow the users to change the field type of any one to any other one in this UI. Up to this point, I simply did not allow the changing of the type once the field was created. But, it seems like we should be able to do this.

I appreciate any help!

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 10:12 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
That problem is known as "mutation", and it has bitten about everyone, I guess ... at least us.

The textbook solution (outside NHibernate) is to change the design so that you have one stable object which points (via composition = "ownership") to the top of an inheritance tree; and you throw away and recreate the child every time someone needs a mutation (see e.g. State and Strategy patterns).

With NHibernate, you have to options, both of which are somewhat unbeautiful:

a) you actually set up two tables, one for the stable objects and one (table-per-hierarchy mapping!) for the children. Not nice because of the joins, but easily done.

b) you use components for the children; unfortunately, NHibernate does not support polymorphic components (as far as I see ...). So we (because that's what we do) wrote our own front-end for components - including a code generator - which creates the necessary mappings and setters and getters on the objects. It's a bit of work ... but if you have only one instance of this problem, a nice manual solution is probably possible.
(Isn't there an mapping for "embedded objects" in Java Hibernate - and is it possibly already there in 1.2 - or might it come one day??).

Regards
Harald


Top
 Profile  
 
 Post subject: Shouldn't this be a bug?
PostPosted: Thu Aug 09, 2007 3:31 pm 
Newbie

Joined: Tue Jun 20, 2006 6:54 am
Posts: 3
In the java version of hibernate, this used to work perfectly. Shouldn't this be a bug in nhibernate ?


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