Hello all.
I have this class that I want to persist with NHibernate. It has one collection, an IList, aggregating strings, rather than class instances, called DestinationCountryCodes.
public class RoutingRule
{
public virtual long Id { get; set; }
public virtual MessageType MessageType { get; set; }
public virtual TransportMethod TransportMethod { get; set; }
public virtual String Sender { get; set; }
public virtual String Recipient { get; set; }
public virtual IList<Party> Parties { get; set; }
public virtual IList<String> DestinationCountryCodes { get; set; }
public virtual string PickupCountryCode { get; set; }
public virtual int PostalCodeLowerLimit { get; set; }
public virtual int PostalCodeUpperLimit { get; set; }
public virtual IList<DbConnectionEntry> DbConnectionEntries { get; set; }
public virtual Boolean Ignore { get; set; }
etcetera....
This is how the mapping looks like:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="FargoGateLib.RoutingRule, FargoGateLib"
table="RoutingRules">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="MessageType" column="MessageType"/>
<property name="TransportMethod" column="TransportMethod"/>
<property name="Sender" column="Sender"/>
<property name="Recipient" column="Recipient"/>
<!--<property name="DestinationCountryCode" column="DestinationCountryCode"/>-->
<bag name="DestinationCountryCodes" table="RoutingRuleCountryCodes" lazy="false" cascade="all">
<key column="RoutingRuleID"/>
<element column="CountryCode" type="System.String" />
</bag>
<property name="PickupCountryCode" column="PickupCountryCode"/>
<property name="PostalCodeLowerLimit" column="PostalCodeLowerLimit"/>
<property name="PostalCodeUpperLimit" column="PostalCodeUpperLimit"/>
<bag name="Parties" table="RoutingRule_Parties" lazy="false" cascade="save-update">
<key column="RoutingRuleID"/>
<many-to-many column="PartyId" class="FargoGateLib.Party, FargoGateLib" />
</bag>
<bag name="DbConnectionEntries" table="RoutingRule_DbConnectionEntries" lazy="false" cascade="save-update">
<key column="RoutingRuleID"/>
<many-to-many column="DbConnectionEntryId" class="FargoGateLib.DbConnectionEntry, FargoGateLib" />
</bag>
<property name="Ignore" column="Ignore"/>
</class>
</hibernate-mapping>
Please note that the bag element's type is set to "System.String".
Now this works fine! NHibernate creates the tables defined and all information is persisted. The problem is that I cannot figure out how to query this by joining the DestinationCountries-bag.
This is my attempt:
IList<RoutingRule> routingRules = session.CreateCriteria(typeof(RoutingRule))
.Add(Restrictions.Eq("Ignore", false))
.Add(Restrictions.Eq("MessageType", MessageType.Booking))
.Add(Restrictions.Eq("TransportMethod", transportMethod))
.Add(Restrictions.Lt("PostalCodeLowerLimit", zipcode))
.Add(Restrictions.Gt("PostalCodeUpperLimit", zipcode))
.Add(Restrictions.Eq("PickupCountryCode", pickupCountry))
.SetFetchMode("DestinationCountryCodes", FetchMode.Join)
.CreateCriteria("DestinationCountryCodes")
.Add(Expression.Eq("CountryCode", destCountry))
.SetFetchMode("Parties", FetchMode.Eager)
.CreateCriteria("Parties")
.Add(Expression.Eq("Identifier", customerIdentifier))
.List<RoutingRule>();
It works fine with only the Parties-join, however, when I add the DestinationCountries-join then I get the following error message:
NHibernate.MappingException: collection was not an association: FargoGateLib.RoutingRule.DestinationCountryCodes
What gives!? I do not really know how to write the query so as to make this work.
Thanks in advance for any tips.