Hibernate EventListeners in Spring konfigurieren

Wer in Spring Hibernate Event Listener eintragen will macht dies folgendermassen (vergleiche auch Spring Dokumentation Kapitel 12.2.2):

<beans>
  <bean id="loadListener" class="org.nautsch.MyLoadEventListener" />

  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
  </bean>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
      <list>
        <value>product.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
    <property name="eventListeners">
        <map>
            <entry key="load"><ref local="loadListener" /></entry>
        </map>
    </property>
  </bean>

</beans>

Wer wissen möchte welche Schlüssel es für die EventListener gibt sollte sich mal den Source der Klasse org.hibernate.event.EventListeners anschauen. Dort gibt es einen statischen Block der alle Keys mit den dazugehörigen Typen definiert:

       static {
                eventInterfaceFromType = new HashMap();

                eventInterfaceFromType.put("auto-flush", AutoFlushEventListener.class);
                eventInterfaceFromType.put("merge", MergeEventListener.class);
                eventInterfaceFromType.put("create", PersistEventListener.class);
                eventInterfaceFromType.put("create-onflush", PersistEventListener.class);
                eventInterfaceFromType.put("delete", DeleteEventListener.class);
                eventInterfaceFromType.put("dirty-check", DirtyCheckEventListener.class);
                eventInterfaceFromType.put("evict", EvictEventListener.class);
                eventInterfaceFromType.put("flush", FlushEventListener.class);
                eventInterfaceFromType.put("flush-entity", FlushEntityEventListener.class);
                eventInterfaceFromType.put("load", LoadEventListener.class);
                eventInterfaceFromType.put("load-collection", InitializeCollectionEventListener.class);
                eventInterfaceFromType.put("lock", LockEventListener.class);
                eventInterfaceFromType.put("refresh", RefreshEventListener.class);
                eventInterfaceFromType.put("replicate", ReplicateEventListener.class);
                eventInterfaceFromType.put("save-update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("save", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("pre-load", PreLoadEventListener.class);
                eventInterfaceFromType.put("pre-update", PreUpdateEventListener.class);
                eventInterfaceFromType.put("pre-delete", PreDeleteEventListener.class);
                eventInterfaceFromType.put("pre-insert", PreInsertEventListener.class);
                eventInterfaceFromType.put("pre-collection-recreate", PreCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("pre-collection-remove", PreCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("pre-collection-update", PreCollectionUpdateEventListener.class);
                eventInterfaceFromType.put("post-load", PostLoadEventListener.class);
                eventInterfaceFromType.put("post-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-commit-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-commit-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-commit-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-collection-recreate", PostCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("post-collection-remove", PostCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("post-collection-update", PostCollectionUpdateEventListener.class);
                eventInterfaceFromType = Collections.unmodifiableMap( eventInterfaceFromType );
        }

One thought on “Hibernate EventListeners in Spring konfigurieren

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s