-->
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: Mapping collection wrapper class that has no table
PostPosted: Mon Nov 28, 2005 12:40 am 
Beginner
Beginner

Joined: Tue Oct 25, 2005 2:44 am
Posts: 20
Location: Sydney, Australia
Hi,

I have a class that forms a wrapper around a collection (to prevent it being deleted from). This class has no table in SQL and no properties except the collection it contains:

Code:
    public class WrapperClass
    {
        private List<CollectionClass> _collectionClasses;

        public int Count
        {
            get { return _collectionClasses.Count; }
        }

        public CollectionClass this[int index]    // Indexer declaration
        {
            get
            {
                return _collectionClasses[index];
            }
        }
   
        public void Add(CollectionClass collectionClass)
        {
            _collectionClasses.Add(collectionClass);
        }

        protected internal WrapperClass()
        {
            _collectionClasses = new Collection<CollectionClass>();
        }
   }


This class in turn is contained in another class which I want to persist and re-retrieve, but I can't get my head around how to map this? How do I map with no sql table?

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 8:37 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
AFAIK, this is not possible...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 5:28 pm 
Beginner
Beginner

Joined: Tue Oct 25, 2005 2:44 am
Posts: 20
Location: Sydney, Australia
So how do other people manage to do this type of thing? Am I just making a poor design decision?


By the way, thanks for doing such a good job on nHibernate; it's going to save me so much time once I understand how to use it properly :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 4:47 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
What exactly are you trying to do?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 6:02 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We do the following to support typed collections. The example shows a Contact class with a collection of ContactAddress objects. Contact and ContactAddress map to a table, but the ContactAddress collection class does not.

Code:
public class Contact
{
    // Accessed directly by NHibernate;
    // must be named the same as the typed public property
    // except using camelcase-underscore
    private IList _addresses;

    private ContactAddressCollection _addressesTyped;

    public virtual ContactAddressCollection Addresses
    {
        get
        {
            if (this._addressesTyped == null)
            {
                // first access of strongly typed collection;
                // wrap weakly typed collection used by NHibernate
                this._addressesTyped = new ContactAddressCollection(this._addresses, ...);
            }
            else
            {
                // Reassign the weakly typed collection to the typed collection
                // just in case NHibernate recreated it ...
                this._addressestyped.ExternalCollection = this.AddressesUntyped;
            }
            return this._addressesTyped;
        }
    }

    // Used only by public typed collection property
    private IList AddressesUntyped
    {
        get
        {
            if (this._addresses == null)
            {
                // Initialize it in case NHibernate hasn't set it yet
                this._addresses = new ArrayList();
            }
            return this._addresses;
        }
    }
}

<hibernate-mapping
    ...
    <class name="Contact" ... >
        ...
        <bag name="Addresses" inverse="true" lazy="true" cascade="all-delete-orphan" access="field.camelcase-underscore">
            <key column="..." />
            <one-to-many class="ContactAddress"/>
        </bag>
    </class>
</hibernate-mapping>


Because of the bag's access of "field", Nhibernate only cares about the weakly typed field, but my property of the same name is strongly typed, so my code and HQL queries can use the property name as expected. From what we can tell so far, it works great.

Our strongly typed collections have a settable property called ExternalCollection, plus a bunch of other stuff to enforce the correct type, track what entity instance the collection belongs to, track the name of the "parent" property on the contained item's type in order to automatically set/clear it when items are added/removed from the collection, raise events before and after items are added/removed, "freeze" the collection so no more items can be added or removed, define arbitrary rules validating an item being added to the collection (based on what's already in it), etc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 8:00 pm 
Beginner
Beginner

Joined: Tue Oct 25, 2005 2:44 am
Posts: 20
Location: Sydney, Australia
Yep, that is exactly what I'm after. Thanks Nels.

Although it'd be nice if nHibernate took care of this for me (table-less mapped classes) as it's not ideal that I have to change my object model to support the peristence framework I've chosen.

Is this something worth considering for Jira, kpixel?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 8:50 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Currently, NHibernate implementation is tighly linked to Hibernate's one; and as this feature doesn't seem to be important for many persons, we aren't likely to add it soon...

Anyway, you can create a JIRA issue and vote it.
And if you submit a patch which enable this feature (without breaking others and with TestCases), we may include it in NHibernate.

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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.