Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Multiple Hibernate Entity Interceptors?

  1. #1
    Join Date
    Aug 2004
    Location
    Brooklyn, NY
    Posts
    20

    Default Multiple Hibernate Entity Interceptors?

    Hello.

    I'm trying to set up two different Entity Interceptors for hibernate using the LocalSessionFactoryBean. It seems that the LocalSessionFactoryBean only allows for one interceptor. Can I use the ProxyFactoryBean to add more? I currently have the following setup:

    <bean id="catalogDS" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiTemplate">
    <ref bean="wlJndiTemplate"/>
    </property>
    <property name="jndiName">
    <value>${jndi.appname}/Catalog</value>
    </property>
    </bean>

    <bean id="CatalogTransactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref local="com.stormhq.entity.CatalogSessionFactory"/>
    </property>
    </bean>

    <!-- my own hibernate interceptor for dirty entity detection -->
    <bean id="unsavedBeanInterceptor" class="com.stormhq.util.HibernateUnsavedEntityInte rceptor"/>

    <bean id="com.stormhq.entity.CatalogSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource">
    <ref bean="catalogDS"/>
    </property>
    <property name="entityInterceptor"><ref bean="unsavedBeanInterceptor"/></property>
    <property name="mappingResources">
    <list>
    <value>com/stormhq/entity/Category.hbm.xml</value>
    <value>com/stormhq/entity/ProductGroup.hbm.xml</value>
    <value>com/stormhq/entity/ProductSKUImpl.hbm.xml</value>
    <value>com/stormhq/entity/attributes/Attribute.hbm.xml</value>
    <value>com/stormhq/entity/attributes/AttributeGroup.hbm.xml</value>
    <value>com/stormhq/lookups/LookupTableImpl.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.O racle9Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <!-- <property name="schemaUpdate"><value>true</value></property> -->

    </bean>


    <bean id="com.stormhq.entity.dao.CategoryDao" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="target"><ref local="com.stormhq.entity.dao.CategoryDAOImpl"/></property>
    <property name="transactionManager"><ref local="CatalogTransactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="load*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>


    Thank you.

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Hibernate SessionFactory allows for using only one entityInterceptor per session.
    If you need to use two entityInterceptors, I suppose they are mutually exclusive so they are not to be used with the same session as Hibernate states, you can create and register a generic entityInterceptor that proxy your base interceptors depending on the usecase.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Brooklyn, NY
    Posts
    20

    Default

    Thanks for the reply.

    They are mutually exclusive, but which interceptor is chosen is determined by the type of entity being used by hibernate and the method call being used. I have 1 interceptor which is used to determine if an entity is saved or not (overrides onSave() and isUnsaved() ) and another one for proxying object creation which overrides instantiate(). I could combine these two since they don't conflict with eachother but that would not be a very elegant solution.


    So you're suggesting that I should create a 3rd object which programatically passes off control to one of the existing interceptors? That would work, but it also seems like a closed solution. Adding new interceptors would require code changes which I'd like to avoid. ProxyFactoryBean allows for a list of interceptors to be registered, but I'm not sure if that could somehow be wrapped around the LocalSessionFactoryBean.

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    but which interceptor is chosen is determined by the type of entity being used by hibernate and the method call being used
    just wondering, how do you manage this when using Hibernate without Spring?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Location
    Brooklyn, NY
    Posts
    20

    Default

    Quote Originally Posted by irbouho
    but which interceptor is chosen is determined by the type of entity being used by hibernate and the method call being used
    just wondering, how do you manage this when using Hibernate without Spring?
    I haven't tried it without spring.

  6. #6
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    AFAIK, Hibernate SessionFactory allows for using only one entityInterceptor per session.
    Code:
       Session openSession&#40;Connection connection, Interceptor interceptor&#41;;
    
       Session openSession&#40;Interceptor interceptor&#41;;
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  7. #7
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    A Session can only have one entity interceptor, so indeed, if you need more, with or without Spring, you need some sort of mechanism (such as a wrapper) that delegates to one or another...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  8. #8
    Join Date
    Aug 2004
    Location
    Brooklyn, NY
    Posts
    20

    Default

    Quote Originally Posted by Colin Sampaleanu
    A Session can only have one entity interceptor, so indeed, if you need more, with or without Spring, you need some sort of mechanism (such as a wrapper) that delegates to one or another...
    Could that wrapper be a ProxyFactoryBean? I notice that LocalSessionFactoryBean doesn't expose a getEntityInterceptor method, but HibernateTransactionManager does. Could interceptors be used to add advice to getEntityInterceptor() in HibernateTransactionManager to provide this functionality?

  9. #9
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I don't know if you really need to use ProxyFactoryBean, since the Hibernate entity Interceptor is just an interface. The wrapper could just be a bean which implements that interfaces and takes a list of the real interceptors, and when it is invoked, delegates to the appropriate one. Ultimately it depends on how that decision is being made, which I guess is a programmatic one... ProxyFactory may help you if you actually need to get at the call context to make the decision...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  10. #10
    Join Date
    Aug 2004
    Location
    Brooklyn, NY
    Posts
    20

    Default

    I see how that would work, but I'm trying to think more long term where we may want to have more than just 2 interceptors and be able to wire them up using IoC instead of programmatically.

    I guess the uber-interceptor could be wired with IoC and do this... but I was hoping there might be a pre-existing spring feature which would make it easier.

Similar Threads

  1. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  2. Multiple Data Sources + Hibernate + Spring
    By joeserel in forum Data
    Replies: 2
    Last Post: May 18th, 2005, 09:22 AM
  3. Other Hibernate DAO LazyInitializationExceptions
    By bernardsirius in forum Data
    Replies: 5
    Last Post: Feb 18th, 2005, 04:09 PM
  4. Replies: 9
    Last Post: Feb 8th, 2005, 09:25 PM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •