Mapping for City:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="KennelFinder.City, KennelFinder" lazy="false">
<composite-id>
<key-property name="Name" column="City"/>
<key-property name="State" column="Statecode"/>
</composite-id>
</class>
<sql-query name="GetCityListByPartialName-CityOnly">
<return alias="City" class="KennelFinder.City, KennelFinder"/>
select distinct
city, statecode
from
geoinformation
where
city like :city
order by
city, statecode
</sql-query>
</hibernate-mapping>
Code for city:
Code:
[Serializable]
public class City
{
private string name;
private string state;
public string Name
{
get { return name; }
set { name = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string FormattedName
{
get { return string.Format("{0}, {1}", name, state); }
}
public override bool Equals(object obj)
{
//Check for null and compare run-time types.
if (obj == null || GetType() != obj.GetType()) return false;
City city = (City)obj;
return name.Equals(city.Name) &&
state.Equals(city.State);
}
public override int GetHashCode()
{
return unchecked(name.GetHashCode() ^ state.GetHashCode());
}
public static IList<City> FindByPartialName(string partialCityName, string partialStateName, int maxCount)
{
if (partialStateName != null && partialStateName.Length > 0)
return FindByPartialCityAndState(partialCityName, partialStateName, maxCount);
else
return FindByPartialCity(partialCityName, maxCount);
}
private static IList<City> FindByPartialCity(string partialCityName, int maxCount)
{
IQuery query = DbSessionContext.Current.Session.GetNamedQuery("GetCityListByPartialName-CityOnly");
query.SetString("city", partialCityName + "%");
query.SetMaxResults(maxCount);
return query.List<City>();
}
private static IList<City> FindByPartialCityAndState(string partialCityName, string partialStateName, int maxCount)
{
IQuery query = DbSessionContext.Current.Session.GetNamedQuery("GetCityListByPartialName-State");
query.SetString("city", partialCityName + "%");
query.SetString("state", partialStateName + "%");
query.SetMaxResults(maxCount);
return query.List<City>();
}
}
The problem:
Running the code with a search for 'eden pr%' returns four results. I'm hitting a geo location table that has four entries for eden prairie, mn so it makes sense (four zip codes, so four records). However, since I'm doing a select distinct on just city and state, I would expect just 1 record.
The executed SQL (obtained from sql trace):
Code:
exec sp_executesql N'WITH query AS (SELECT DISTINCT TOP 20 ROW_NUMBER() OVER (order by
city, statecode) as __hibernate_row_nr__,
city, statecode
from
geoinformation
where
city like @p0
order by
city, statecode) SELECT * FROM query WHERE __hibernate_row_nr__ > 0 ORDER BY __hibernate_row_nr__',N'@p0 nvarchar(8)',@p0=N'eden pr%'
Now, if I change the dialect to SQL Server 2000, everything works correctly. In that case, the resulting SQL is simply:
Code:
exec sp_executesql N'select distinct top 20
city, statecode
from
geoinformation
where
city like @p0
order by
city, statecode',N'@p0 nvarchar(8)',@p0=N'eden pr%'
My web.config settings:
Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="KennelFinder">
<property name="hibernate.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="hibernate.connection.connection_string">Server=(local);initial catalog=mobyweb;Trusted_Connection=true;</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="hibernate.connection.isolation">ReadCommitted</property>
<property name="hibernate.use_proxy_validator">False</property>
<property name="hibernate.default_schema">mobyWeb.dbo</property>
<mapping assembly="KennelFinder"/>
</session-factory>
</hibernate-configuration>
I should point out, I noticed the same error in beta 1, which prompted the upgrade to beta 2.