NHibernate - использование свойств из набора в под-выборе - PullRequest
1 голос
/ 22 января 2012

У меня есть следующее отображение:

<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

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 = 'Name 'и x.LocalizedEntityClass =' ​​Prayon.Entities.Country 'и x.CultureName =' de 'take 1) Like: val]

Я надеюсь, что кто-то может помочь мне, как настроить мой hql.

Ответы [ 2 ]

0 голосов
/ 08 февраля 2012

Любой тип "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 - ключ к решению вашей непосредственной задачи.

0 голосов
/ 25 января 2012

Как насчет:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...