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.  [ 1 post ] 
Author Message
 Post subject: Mapping multiple properties of the same user-defined type
PostPosted: Wed Sep 01, 2010 5:00 am 
Newbie

Joined: Wed Sep 01, 2010 4:40 am
Posts: 1
For the project I'm currently participating in, I need to add mapping files for new added classes. (As a way of learning to use nHibernate)

I have a class which defines many properties of the same type. This class holds some financial information and uses a 'Money' class to store the amount and currency for each of the properties. (like TotalAmount, UpperLimitAmount, etc. 29 Money properties in total).

public class CalculationResultRecord // The class i need to map
{
...
public Money TotalAmount { get; set; }
public Money UpperLimitAmount { get; set; }
...
}

public class Money // A class used in many other classes throughout the rest of the project
{
...
public decimal Amount { get; set; }
public Currency Currency { get; set; }
...
}

This design does not come from me nor can i change anything about it. I just need to add mapping files ...

I've tried to map each property as a 'Money' component like this.

<class name="CalculationResultRecord" lazy="false" where="Deleted='false'" >
...
<component name="TotalAmount">
<property column="TotalAmount" name="Amount"/>
<many-to-one name="Currency" outer-join="false" cascade="none"/>
</component>
<component name="UpperLimitAmount">
<property column="UpperLimitAmountAmount" name="Amount"/>
<many-to-one name="Currency" outer-join="false" cascade="none"/>
</component>
...
</class>

This will put the value for every '...Amount' in a seperate correctly named column. But since the currency will be the same for all properties, (will be enforced by validationrules), I would only need one column which specifies the currency (a reference to the currencies table actually).

With the above mapping only one 'Currency' column is created in the database but this does not work. NHibernate throws the following error during tests:

System.IndexOutOfRangeException: Invalid index 33 for this SqlParameterCollection with Count=33

As far as I understand here nHibernate still looks for seperate columns for all the 'Currency' fields while in fact it should look to only one column. Even if i add a column attribute to all the many-to-one elements, and then referencing the same columnname, the same error pops up. My guess is there must be something fundamentaly wrong that I'm doing.

Is the mapping strategy that i'm trying for this class possible? If not what would be the best way to map this class or could u point me in the right direction to go? With my still limited understanding of nHibernate and ORM in general, i can't think of another way than to map those properties as components.

Many thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.