By implementing Hibernate Interceptor interface or by implementing org.hibernate.classic.Validatable or by implementing org.hibernate.classic.Lifecycle , you can receive the various callbacks from a hibernate "session".
Using these callbacks you could inspect persistent objects before they are saved, deleted, updated or loaded.
- Interceptor Approach
- Interceptor could be done at a session scope or at session factory level or at persistent class level.
- If its done at session factory level, interceptors must be thread safe, taking care to not store session-specific state since multiple sessions will use this interceptor (potentially) concurrently.
- At session level, one instance per session.
- To implement a Interceptor 2 ways
- Implement interface org.hibernate.Interceptor
- Or extend org.hibernate.EmptyInterceptor
- Once interceptor is implemented register it programatically with session or session factory
- Register the interceptor with Session
Session session = sf.openSession( new MyInterceptor());
- Register the interceptor with SessionFactory
new Configuration().setInterceptor( new MyInterceptor() );
- Integration with spring ?
<bean id="myInterceptor" class="com.company.MyInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="entityInterceptor">
<ref bean="myInterceptor">
</ref>
</property></bean>
- Pros & cons
- Gives flexibility to do things at session and/or at entire application level.
- Could be a bottleneck if interceptor is being used at SessionFactory level, code inside interceptor has to be written very-2 carefully to make sure it is efficient. --- Things we may end up doing are logging changes to specific database tables in a database or to a log file(files is not a good idea).
- Would be nice if the interceptors could turned on and off based on configuration files, instead of programatically enabling it.? --- This could be achieved by modifying SPRING configuration file.(applicationContext.xml)
- To log for specific list of persistence entities there is no out of box configuration, one would have to create additional configuration parameters ?
No comments:
Post a Comment