views:

31

answers:

1

Hi,

I'm trying to load nhibernate mapping for POCO classes at runtime with following lines:

var persistentClass = NHibernateHelper.Configuration.GetClassMapping( type );
var property = persistentClass.GetProperty( propertyName );

It works fine except it fails on property GroupId on a class with following mapping:

<class name="GroupPartnerInterest" table="[GROUP_PARTNER_INTERESTS]">
  <composite-id >
    <key-property name="GroupId" column="PAR_ID" />

If type == typeof(GroupPartnerInterest) persistentClass.GetProperty( "GroupId" ) fails with MappingException:

property not found: GroupId on entity GroupPartnerInterest"

I can see in debugger that key-properties from composite-id do not appear in persistentClass.properties.

Is there a way to get mapping for this key-property?

Thank you in advance.

A: 

The ordinary properties can be iterated through persistentClass.PropertyClosureIterator (that is including properties from base classes).

The key properties are in ( ( Component )( persistentClass.Identifier ) ).PropertyIterator.

So with this piece of code I'm able to search both key properties and ordinary properties:

var propserties = persistentClass.PropertyClosureIterator;
if ( persistentClass.Identifier is Component )
{
    properties = ( ( Component )( persistentClass.Identifier ) ).PropertyIterator
                    .Union( properties );
}

Property property
    = (
        from it in properties
        where it.Name == propertyName
        select it
      ).FirstOrDefault();
zzandy