I’ve got a record I’m editing in a ASP.net web app using nhibernate and MVP. When I make the changes to the record, I need to check to see that one field is unique (see ‘friendlyUrl’ below). However when I so my Criteria.List call, it updates the record as well as all child records (PharmacyStoreHours) first then does the select (found this out with a SQL profile). Obviously I don’t want to update the record before the validation is complete and the validation fails as the data has been updated before the select. How do I do a select without the record updating?
As this is my first project with NHibernate, I’m not sure what would be causing this. I’ve copied what I’ve done from other parts of the code so if things look funny, please explain why they are so I can better understand what I should be doing.
NHibernate Config File Class:
<class table="Pharmacy" name="DataAccess.Domains.Pharmacy, DataAccess">
<id name="PharmacyId" column="PharmacyId" unsaved-value="0">
<generator class="increment" />
</id>
<property name="StoreAccountNumber" column="StoreAccountNumber" type="System.String" not-null="true" />
<property name="ClientName" column="ClientName" type="System.String" not-null="true" />
<property name="PharmacyName" column="PharmacyName" type="System.String" not-null="true" />
<property name="Address" column="Address" type="System.String" not-null="false" />
<property name="AddressContinued" column="AddressContinued" type="System.String" not-null="false" />
<property name="City" column="City" type="System.String" not-null="false" />
<property name="State" column="State" type="System.String" not-null="false" />
<property name="Zipcode" column="Zipcode" type="System.String" not-null="false" />
<property name="Phone" column="Phone" type="System.String" not-null="false" />
<property name="Fax" column="Fax" type="System.String" not-null="false" />
<property name="Email" column="Email" type="System.String" not-null="false" />
<property name="FriendlyUrl" column="FriendlyUrl" type="System.String" not-null="false" />
<property name="OwnersName" column="OwnersName" type="System.String" not-null="false" />
<property name="Latitude" column="Latitude" type="System.Decimal" not-null="false" />
<property name="Longitude" column="Longitude" type="System.Decimal" not-null="false" />
<bag name="Hours" table="PharmacyStoreHours" cascade="all" inverse="true">
<key column="PharmacyId" />
<one-to-many class="DataAccess.Domains.PharmacyWorkDays, DataAccess" />
</bag>
</class>
<class table="PharmacyStoreHours" name="DataAccess.Domains.PharmacyWorkDays, DataAccess">
<id name="PharmacyStoreHourId" column="PharmacyStoreHourId" unsaved-value="0">
<generator class="increment" />
</id>
<property name="WorkDay" column="WorkDay" type="System.Int32" not-null="true" />
<property name="OpenTime" column="OpenTime" type="System.DateTime" not-null="true" />
<property name="CloseTime" column="CloseTime" type="System.DateTime" not-null="true" />
<many-to-one name="PharmacyMap" class="DataAccess.Domains.Pharmacy, DataAccess" column="PharmacyId" not-null="true" />
</class>
Code:
public bool IsFriendlyUrlUnique(string clientName, string friendlyUrl)
{
bool result = false;
ICriteria crit = CurrentSession.CreateCriteria(typeof (Pharmacy));
crit.Add(new EqExpression("ClientName", clientName));
crit.Add(new EqExpression("FriendlyUrl", friendlyUrl));
crit.AddOrder(Order.Asc(PHARMACY_NAME));
if (crit.List<Pharmacy>().Count == 0)
result = true;
return result;
}
Thanks in advance for the help.
Advised Solution
I would advise, in the query, you filter out the current record.
Ideally, you should perform this validation before you alter the entity itself. Also see Rune’s answer about placing a constraint in the DB to enforce this constraint. This check is not to prevent duplicates, its to try and detect them earlier so you can present a nicer error message and avoid trying to commit a change that you know will fail.
Why this is happening (FlushMode)
This is on purpose to ensure the results from your query are consistent with the edits you have made locally. Since NHibernate is trying to model your entities as if they are always available in memory by automatically persisting to/from the database as required.
You can change this by looking at the auto flush setting. There is a default value on the session factory used for any newly created sessions, and an individual setting on each session.
The default value is
FlushMode.Autowhich will save when a transaction is committed or before running any query that might be affected by changes in memory. If you change it toFlushMode.Committhen it will only persist changes back to the database when committing a transaction, or when saving a new entity that uses a primary key strategy such as identity.You can also manually persist the changes in the current session back to the database at any time using
Session.Flush.