Hibernate Interceptor and Event

本文介绍了Hibernate中拦截器和事件系统的使用方式及区别。拦截器分为会话范围和会话工厂范围,前者在打开会话时指定,后者需在配置阶段设置,并应用于所有会话。事件系统则通过配置文件定义,可用于替代或补充拦截器的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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????

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值