Hibernate Interceptor and Event

来源:互联网 发布:java win32 api 编辑:程序博客网 时间:2024/05/06 09:11

Interceptor:

Interceptors come in two flavors: Session-scoped and SessionFactory-scoped.  

A Session-scoped interceptor is specified when a session is opened using one of the overloaded SessionFactory.openSession() methods accepting an Interceptor:

Session session = sf.openSession( new AuditInterceptor() );  

  A SessionFactory-scoped interceptor is registered with the Configuration object prior to building the SessionFactory. In this case, the supplied interceptor will be applied to all sessions opened from that SessionFactory; this is true unless a session is opened explicitly specifying the interceptor to use. SessionFactory-scoped interceptors must be thread safe, taking care to not store session-specific state since multiple sessions will use this interceptor (potentially) concurrently.

new Configuration().setInterceptor( new AuditInterceptor() );

 

Event:

The event system can be used in addition or as a replacement for interceptors.

Event was defined in hibernate.cfg.xml, after interceptor created:

<hibernate-configuration>    <session-factory>        ...        <event type="load">            <listener class="com.eg.MyLoadListener"/>            <listener class="org.hibernate.event.def.DefaultLoadEventListener"/>        </event>    </session-factory></hibernate-configuration>

 

But what's the really different between Interceptor and Event????

 

原创粉丝点击