2012-01-22 128 views
1

从一组属性我有休耕映射:NHibernate的 - 使用子查询

<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应全部归还国家,与下面的“正常”SQL-选择

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 [A国,其中

未能解决物业(选择从的PropertyValue LocalizedProperty X 其中x.EntityId = a.Id和x.PropertyName = '名称' 和x.LocalizedEntityClass = 'Prayon.Entities.Country' 和x.CultureName = '日' 取1)像:VAL ]

我希望有人能帮助我如何设置我的HQL。

回答

0

任何“任何”型有两个特殊的属性“id”和“类”,所以你应该能够做一些与他们这样的

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是解决你的眼前挑战的关键。

+0

很好,它与你的语句一起工作1:1。谢谢! – BennoDual

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 
+0

这给了我一个TypeMismatchException与休闲信息:二进制逻辑运算符的左侧和右侧不兼容[Object:Prayon.Entities.Country] :-( – BennoDual