爲了設置事件偵聽器,您需要添加以下代碼初始化NHibernate的時候:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
//Load configuration
//Add NHibernate.Search listeners
cfg.SetListener(NHibernate.Event.ListenerType.PostUpdate, new FullTextIndexEventListener());
cfg.SetListener(NHibernate.Event.ListenerType.PostInsert, new FullTextIndexEventListener());
cfg.SetListener(NHibernate.Event.ListenerType.PostDelete, new FullTextIndexEventListener());
var factory = cfg.BuildSessionFactory();
你的web.config/app.config文件必須被更改爲包含以下內容:
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
<section name="nhs-configuration" type="NHibernate.Search.Cfg.ConfigurationSectionHandler, NHibernate.Search" requirePermission="false"/>
</configSections>
<!-- NHibernate.Search -->
<nhs-configuration xmlns='urn:nhs-configuration-1.0'>
<search-factory>
<property name='hibernate.search.default.directory_provider'>NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search</property>
<property name='hibernate.search.default.indexBase'>PATH TO LUCENE.NET STORE</property>
<property name='hibernate.search.indexing_strategy'>event</property>
</search-factory>
</nhs-configuration>
<appSettings>
<add key="Lucene.Net.lockdir" value="SAME PATH AS ABOVE" />
</appSettings>
...
最後:當你創建一個ISession實例時,記得使用這段代碼來獲得一個IFullTextSession。
IFullTextSession session = Search.CreateFullTextSession(factory.OpenSession());
這應該適用於Lucene 2.0和NHibernate 2.0。
也許這不是正確的地方問這種問題 - 你有沒有嘗試NHibernate的貢獻組 - http://groups.google.com/group/nhcdevs/? – Rashack 2009-04-17 09:46:41