NHibernate Pitfalls: Specifying Event Listeners in XML Configuration

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

If you specify an event listener in XML configuration (App.config, Web.config, hibernate.cfg.xml) you are replacing the NHibernate standard event listener, not adding your own:

   1: <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   2:         <bytecode-provider type="lcg"/>
   3:         <reflection-optimizer use="true"/>
   4:         <session-factory>
   5:             <property name="adonet.batch_size">200</property>
   6:             <property name="adonet.wrap_result_sets">false</property>
   7:             <property name="cache.use_query_cache">true</property>
   8:             <property name="command_timeout">20</property>
   9:             <property name="connection.connection_string_name">NHibernate</property>
  10:             <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  11:             <property name="connection.isolation">ReadCommitted</property>
  12:             <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
  13:             <property name="format_sql">true</property>
  14:             <property name="generate_statistics">false</property>
  15:             <property name="hbm2ddl.keywords">none</property>
  16:             <property name="query.startup_check">false</property>
  17:             <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  18:             <property name="show_sql">true</property>
  19:             <mapping assembly="FlushTest"/>
  20:             <listener type="flush-entity" class="FlushEntityEventListener, MyAssembly"/>
  21:         </session-factory>
  22:     </hibernate-configuration>

If you want to do it by configuration file, make sure you also add NHibernate’s, but you should instead be doing it by code:

   1:             Configuration cfg = new Configuration().Configure();
   2:             cfg.EventListeners.FlushEntityEventListeners = cfg.EventListeners.FlushEntityEventListeners.Concat(new[] { new FlushEntityEventListener() }).ToArray();
Bookmark and Share

                             

No Comments