My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
NHibernateIntegration  
Explains SolrNet-NHibernate integration
Updated Mar 24, 2012 by mauricio...@gmail.com

NHibernate integration

A NHibernate-SolrNet module is included in SolrNet, with these features:

  • Automatic database -> Solr synchronization
  • Issue Solr queries from NHibernate's ISession

This is intended to be used on entities that are similarly mapped on both NHibernate and SolrNet.

Setup

Configure SolrNet and NHibernate as usual. Then apply SolrNet's integration to NHibernate's configuration like this:

NHibernate.Cfg.Configuration cfg = SetupNHibernate();
var cfgHelper = new NHibernate.SolrNet.CfgHelper();
cfgHelper.Configure(cfg, true); // true -> autocommit Solr after every operation (not really recommended)

If you're not using the default built-in container, you have to tell CfgHelper to use it, e.g.:

IWindsorContainer container = new WindsorContainer();
...
var cfgHelper = new NHibernate.SolrNet.CfgHelper(container);
...

Usage

Whenever a NHibernate entity that is also mapped in SolrNet is created, updated, or deleted, it will be automatically updated on Solr. NHibernate transactions are honored: entities are updated on Solr only when the NHibernate transaction is committed.

This synchronization goes only in the direction NHibernate -> SolrNet, not the other way around, i.e. operations issued directly through ISolrOperations will not be reflected on NHibernate.

In order to issue Solr queries through NHibernate, the ISession needs to be wrapped. Sample:

ISessionFactory sessionFactory = ...
using (var session = cfgHelper.OpenSession(sessionFactory)) {
   ICollection<Entity> entities = session.CreateSolrQuery("this is a full-text query").SetMaxResults(10).List<Entity>();
}
Powered by Google Project Hosting