views:

2411

answers:

5

Hi,

I have a following class:

MyClass
public virtual int Id { get; set; }
public virtual int Code { get; set; }
public virtual int Description { get; set; }
public virtual int Name { get; set; }

with the following mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TestApplication" assembly="TestApplication">
  <class name="MyClass" table="MyTable">
    <id name="Id" column="id">
      <generator class="native"/>
    </id>

    <property name="Code" column="code"/>
    <property name="Description" column="description"/>
    <property name="Name" formula="(SELECT b.translation FROM translations b WHERE b.translation_id = translation_id AND b.language_id = :TranslationFilter.LanguageId)"/>
  </class>

  <filter-def name="TranslationFilter">
    <filter-param name="LanguageId" type="Int32"/>
  </filter-def>
</hibernate-mapping>

I'm trying to load entity through spring with:

Session.EnableFilter("TranslationFilter").SetParameter("LanguageId", 1);
return Session.Get<MyClass>(1);

but I'am getting adoexception. I see (in a profiler) that variable :TranslationFilter.LanguageId is not replaced with ? and that parameter value is not send to the server?

Is it this possible (to have filters in formula) and how?

Many thanks!

A: 

This should be possible,

Oren gives an example on his blog:

http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx

Remco Ros
:) I read this blog post several times (before posting here), to see where I am wrong, but I couldn't see the difference. But it is interesting that someone on nhforge said that parameters in formulas are not supported...
rrejc
A: 

Same problem, if I have a formula and set a condition of the formula field with ICriteria, resulting query gets no filter substitution on the query, and the query issued to the database is wrong.

I'll investigate on the issue and try to submit bug in nhibernate jira.

alk.

Alkampfer
A: 

I have encountered the same issue;

The problem seems to be related to HQL queries (as the filter-def parameter is not set on the query to your db).

If you use a session.CreateCriteria it does work (at least for me)!

chris
A: 

I have done it in same way and it is working without exception. And Dont forget to disable filter if it is not in further use. Keep Psoting :)

Surya
A: 

This feature is not officially supported. As such oren's blog post about this combination of 2 different features (formulas and filters) should be taken with a grain of salt...

Jaguar