KingTravisG
KingTravisG

Reputation: 1336

NHibernate - getting one-to-many

Wondering if anyone could point me in the right direction..

I currently have the following 3 tables in my sqlserver database

Parameter ParameterId ParameterName

ParameterValue ParameterValueId ParameterValue

ParameterParameterValue ParameterId ParameterValueId

I'm trying to get it where the Parameter domain object will also fetch all the ParameterValue objects as well (I'm guessing Parameter has a one-to-many relationship with ParameterValue, since a parameter can have more than one value) but I'm getting no where - the msot I've achieved is fetching the first value, rather than all :(

If anyone is willing to help or anything I can post some code and/or the mappings I'm using - as always, any help is much appreciated :)

Mappings for Parameter

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
               namespace="StockMarketAdvisorDatabaseAccess.Domain" 
               assembly="StockMarketAdvisorDatabaseAccess">
<class name="Parameter" table="Parameter">
        <id name="ParameterId">
            <column name="ParameterId" sql-type="int" not-null="true" />
            <generator class="identity" />
        </id>
        <property name="ParameterName" />
        <bag name="ParameterValues" table="ParameterParameterValue" cascade="none">
            <key column="ParameterValueId" />
            <one-to-many class="ParameterValue" />
        </bag>
    </class>
</hibernate-mapping>

Mappings for ParameterValue

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   namespace="StockMarketAdvisorDatabaseAccess.Domain" 
                   assembly="StockMarketAdvisorDatabaseAccess">
    <class name="ParameterValue" table="ParameterValue">
        <id name="ParameterValueId">
            <column name="ParameterValueId" sql-type="int" not-null="true" />
            <generator class="identity" />
        </id>
        <property name="Value" column="ParameterValue"/>
     </class>
</hibernate-mapping>

Thanks again, just started using nHibernate so still trying to figure most it out! :)

Upvotes: 0

Views: 226

Answers (1)

Steve Py
Steve Py

Reputation: 35073

Your issue is you are describing one thing (1-many) but your table structure is another (many-many)

If you need many values per parameter then you should simplify your table structure to:

Parameter
-------------
* ParameterId
ParameterName


ParameterValue
--------------------
* ParameterValueId
ParameterId
ParameterValue

Then your mapping can use a 1-many mapping:

<class name="Parameter" table="Parameter">
    <id name="ParameterId">
        <column name="ParameterId" sql-type="int" not-null="true" />
        <generator class="identity" />
    </id>
    <property name="ParameterName" />
    <bag name="ParameterValues" table="ParameterValue" cascade="none">
        <key column="ParameterId" />
        <one-to-many class="ParameterValue" />
    </bag>
</class>

Upvotes: 1

Related Questions