Search code examples
nhibernatenhibernate-mapping

Session.Get and Session.Load commands ignore filters


I'm using a mapping with a filter.

But when I try to persist my object, it first wants to get a snapshot (because my Id is a string).

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DomainLayer.General" assembly="DomainLayer">
  <class name="Fund" table="OPENA_BriefW" lazy="false">
    <id name="Id" column="`BRWNUMMER`" type="string" >
    </id>

    <property name="Name" column="`BRWNAAM`" type="string"  />
    <property name="Contact" column="`BRWNAAM2`" type="string"  />
    <property name="Address" column="`BRWADRES`" type="string"  />
    <property name="City" column="`BRWSTAD`" type="string"  />
    <property name="Zip" column="`BRWPOST`" type="string" />
    <property name="Phone" column="`BRWTELEFOON`" type="string" />
    <property name="Fax" column="`BRWTELEFAX`" type="string" />
    <property name="Iban" column="`brw_iban`" type="string" />
    <property name="BankAccount" column="`BRWBANKNU`" type="string" />
    <property name="Swift" column="`brw_swift`" type="string" />
    <property name="ReceiveOffice" column="`BRWONTVKANT`" type="int" />
    <property name="RegionDirection" column="`BRWGEWESTDIR`" type="int" />

    <many-to-one name="Country" class="DomainLayer.General.CodeDescription" fetch="join" not-found="ignore">
      <formula>'ALG'</formula>
      <formula>'0'</formula>
      <formula>'WG030'</formula>
      <column name="`BRWLAND`" />
      <formula>:LanguageFilter.Id</formula>
    </many-to-one>

  </class>
</hibernate-mapping>

As you can see the filter :LanguageFilter.Id is the one causing the troubles. When I do a normal .List() it doesn't cause problems.

But when I persist, it first wants to check if the Fund already exists yes/no. By doing a .Get (with the Id).

Then I get the error can't retrieve snapshot because in my query he doesn't replace :LangaugeFilter.Id with the effective value i set on my session.

I enable the filter on my session like so:

session.EnableFilter("LanguageFilter").SetParameter("Id", 1);

Here's the filter-def mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <filter-def name='LanguageFilter' >
    <filter-param name='Id' type='System.Int32' />
  </filter-def>
</hibernate-mapping>

This post (comment 4) http://ayende.com/blog/3993/nhibernate-filters says that session.Get and Load ignores filters.

Are there any alternatives, because I need that language to be variable.


Solution

  • Ok, what I did was the following: I didn't use SaveOrPersist, but Save when new and Persist when I had an old one. This didn't execute the extra get.