NHibernate - 使用subselect中的set属性

时间:2012-01-22 15:16:47

标签: c# nhibernate hql

我有下面的映射:

<class name="Country" table="Country" lazy="false"  >
  <cache usage="read-write"/>
  <id name="Id" column="Id" type="Guid">        
                <generator class="assigned"/>
  </id>
  <property name="Name" column="Name" type="String" length="50" not-null="true" unique="true"  />
  <set name="LocalizedProperties" where="LocalizedEntityClass = 'Prayon.Entities.Country'" cascade="delete">
    <key column="EntityId" foreign-key="none" />
    <one-to-many class="LocalizedProperty" />
  </set>
</class>

LocalizedProperty声明如下:

<class name="LocalizedProperty" table="LocalizedProperty">
 <cache usage="read-write"/>
  <id name="Id" column="Id">
    <generator class="guid.comb"/>
  </id>
  <property name="CultureName"  not-null="true"/>
  <property name="PropertyName"  not-null="true"/>
  <property name="PropertyValue"  not-null="true"/>

  <any id-type="Guid" name="Entity">
    <column name="LocalizedEntityClass"  not-null="true"/>
    <column name="EntityId"  not-null="false"/>
  </any>
</class>

现在我尝试创建一个带有hql的select,它应该返回所有的国家/地区,其中包括“正常”的SQL-Select

select * 
from Country a 
where (
  select top 1 PropertyValue 
  from LocalizedProperty x 
  where x.EntityId = a.Id 
    and x.PropertyName = 'Name' 
    and x.LocalizedEntityClass = 'Prayon.Entities.Country' 
    and x.CultureName = 'de') 
  Like 'a%'

当我创建像

这样的hql时
from Country a 
where (
  select PropertyValue 
  from LocalizedProperty x 
  where x.EntityId = a.Id 
    and x.PropertyName = 'Name' 
    and x.LocalizedEntityClass = 'Prayon.Entities.Country' 
    and x.CultureName = 'de' take 1) 
  Like :val

并将参数val设置为%

我得到以下QueryException

  

无法解析属性:EntityId:Prayon.Entities.LocalizedProperty    [来自Country a where(从LocalizedProperty x中选择PropertyValue)     其中x.EntityId = a.Id和x.PropertyName ='Name'和x.LocalizedEntityClass ='Prayon.Entities.Country'     和x.CultureName ='de'取1)喜欢:val]

我希望有人可以帮我解决如何设置我的hql。

2 个答案:

答案 0 :(得分:0)

怎么样:

from Country a 
where (
  select PropertyValue 
  from LocalizedProperty x 
  where x.Entity = a
    and x.PropertyName = 'Name' 
    and x.CultureName = 'de' take 1) 
  Like :val

答案 1 :(得分:0)

任何“any”类型都有两个特殊属性“id”和“class”,所以你应该可以像这样对它们做些什么

from Country a 
where (
  select PropertyValue 
  from LocalizedProperty x 
  where x.Entity.id = a.Id 
    and x.PropertyName = 'Name' 
    and x.Entity.class = 'Prayon.Entities.Country' 
    and x.CultureName = 'de' take 1) 
  Like :val

我不是100%确定以上是正确的,因为我没有看到你到底在做什么。我认为,关于你是否真的需要任何一种你似乎正在做的本地化的讨论会很好。

尽管如此,我非常确定.id和.class是解决您当前挑战的关键。