8

我需要创建一个命名查询,并将其与我目前已定义为流利地图的其中一张地图一起使用。

是否可以继续使用流利的地图,并能够在代码中动态创建命名查询?或者,切换到 hbm 地图是唯一的选择吗?

4

1 回答 1

10

Maybe I'm misreading the question, but you don't have to switch to hbm mapping completely.

You could continue to use fluent NHibernate to map classes and use hbm for named queries only. In your configuration, you'd then include the entities and the hbms.

_sessionFactory = Fluently.Configure()
.Mappings(m =>
{
   m.FluentMappings.AddFromAssemblyOf<SomeEntityMap>();
   m.HbmMappings.AddFromAssemblyOf<SomeEntityMap>();
})
.BuildSessionFactory();

In your namedQueries.hbm.xml you then only define named queries:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<query name="Some.Query.Of.Yours">
<![CDATA[
          from SomeEntity e
          where  e.Property = :propertyValue
          ]]>
</query>
</hibernate-mapping>
于 2010-09-04T05:12:12.797 回答