NHibernate:查询字符串集合(集合不是关联)

时间:2010-06-18 21:30:51

标签: nhibernate string collections

我有这个类,我想坚持使用NHibernate。它有一个集合,一个IList,聚合字符串,而不是类实例,称为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 ....

这是映射的样子:

<?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>

请注意,bag元素的类型设置为“System.String”。

现在这个工作正常! NHibernate创建定义的表,并保留所有信息。问题是我无法通过加入DestinationCountries-bag来弄清楚如何查询。

这是我的尝试:

   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>(); 

只有Parties-join才能正常工作,但是,当我添加DestinationCountries-join时,我收到以下错误消息:

NHibernate.MappingException: collection was not an association: FargoGateLib.RoutingRule.DestinationCountryCodes

什么给了!?我真的不知道如何编写查询以使其工作。

提前感谢任何提示。

0 个答案:

没有答案