Announcement Announcement Module
Collapse
No announcement yet.
How Common to Spring Manage Beans Created By Hibernate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #31
    See org.springframework.orm.hibernate.support.Dependen cyInjectionInterceptorFactoryBean in the sandbox. Configuration is via the setter methods on the DependencyInjectionAspectSupport superclass. These would be shared with JDO/other implementations to allow consistent configuration.

    In case you're wondering why it's a factory bean, rather than a plain object as in Oliver's contribution--I wanted to extend ChainedInterceptorSupport as well as DependencyInjectionAspectSupport. As that's not possible in Java, I used a trick where inheritance from DependencyInjectionAspectSupport is via a FactoryBean (enabling inheritance of the config parameters that might be shared with JDO, aspect-based implementations, even possibly implementations not concerned with persistence), and inheritance from ChainedInterceptorSupport is handled within the return of getObject().

    As this class is a Spring infrastructure class, IMO it's no problem that it depends on the Spring IoC container FactoryBean feature.

    Rgds
    Rod

    Comment


    • #32
      As far as I can see there is a little problem. Spring offers two points for consistent (net.sf.hibernate.)Interceptor injection, the LocalSessionFactoryBean and the HibernateTransactionManager (via the entityInterceptor properties). I prefer to use the LocalSessionFactoryBean way. But configuring the DependencyInjectionInterceptorFactoryBean with a LocalSessionFactoryBean via a Spring XML configuration is not possible: Each needs the other as a dependency, yielding a circle.

      For the time being I use the following work-around: I have my LocalSessionFactoryBean reference a dummy-implementation of the ChainedInterceptorSupport abstract class. After both the LocalSessionFactoryBean and the DependencyInjectionInterceptorFactoryBean are set up, I use a simple "connector" class, which adds the Interceptor created by the DependencyInjectionInterceptorFactoryBean to the interceptor chain of the dummy-implementation.

      Comment


      • #33
        Good point. Unfortunately I ran out of time to work on this, and only got as far as unit testing. Hopefully I'll have time to take another look at it next week.

        Comment


        • #34
          Originally posted by Ben Alex
          In summary it is helpful to break "business logic" into its two forms: domain logic and application logic. Domain logic deals with the problem domain alone and should be in the business objects. Application logic deals with the application responsibilities (gateways, DAOs etc) and should be in service objects.
          If not mistaken, i guess what you mean is that querying through dao should be in the service logic layer, while the actuall execution that has business sense should be in the domain object, something like below, no?

          Code:
          class PersonManager() {
          	IPersonDao get/setPersonDao();
          
          	List findRelativesOfPersonById(Long personId) { 
          		Person p = getPersonDao.findPerson(personId);
                          Stirng someOtherParam = // some other parameters
                          OtherInfo otherInfo = // query Dao for other information needed
                          p.findRelativesOfPerson(someOtherParams, otherInfo);
          	}
          	
          }
          
          class Person {
          	Long get/setId();
          	String get/setName();
          	...etc...
          
          	// behavioral methods
          	List findRelativesOfPersonById(String someparam, OtherInfo otherInfo) {
          		// do business logic here
          	}
          }
          In this case, the service(facade) has to deal with transaction (through aop) and also dao querying but the actuall business handling is still in the domain object (business object) such that the domain object is no longer a fake one. I think this is also another alternative. Any comments? How does this compare to the code where Dao access are in the domain object and using Hibernate's Interceptor to get bean (with dependecies injected)?

          Thanks in advance

          regards
          tmjee

          Comment


          • #35
            Rod, sicarius,

            when I originally implemented SpringInterceptor I also ran into the circular dependency between the interceptor and the session factory. I got around it by allowing the interceptor to take the name of the session factory bean or the actual bean. If the bean name was provided it would then lazily look it up from the BeanFactory only when it was needed.

            Also it doesn't appear that it's possible to specify "next interceptor" for the interceptor created by DependencyInjectionInterceptorFactoryBean. Do you need to copy the setNextInterceptor(Interceptor nextInterceptor) method from ChainedInterceptorSupport into DependencyInjectionInterceptorFactoryBean?

            Ollie

            PS. Rod, I really like having ability to simply auto-wire the persistent objects rather that have to define prototypes in the bean factory. Very nice.

            Comment


            • #36
              Rod,

              I created an issue in Jira and attached a patch that resolves the circular dependency (as per my last post) and lack of setter for next interceptor issues.

              http://opensource.atlassian.com/proj...browse/SPR-382

              I wish we could add attachments to the forum....

              HTH

              Ollie

              Comment


              • #37
                Thanks Ollie. I'll look at your patch and resolve the circular dependency today or next week. Yes, the next interceptor will need to be a property on the enclosing FactoryBean.

                Comment


                • #38
                  I've fixed this and closed the JIRA issue. The fix uses a name property for the SessionFactory, like Oliver's original contribution.

                  Comment


                  • #39
                    summary

                    Can one of you summarise how to avail of this service, as most of you seem to be very familiar with Spring's architecture? Is this something that will make it into Spring 1.2, or will it remain in the sandbox for some time?

                    TIA

                    Comment


                    • #40
                      Can one of you summarise how to avail of this service, as most of you seem to be very familiar with Spring's architecture?
                      The fundamental configuration step is to add an appropriately configured DependencyInjectionInterceptorFactoryBean as a Hibernate interceptor in your Hibernate SessionFactory. Do this via the "entityInterceptor" property of the LocalSessionFactoryBean, for example. A trivial example:

                      Code:
                      <bean id="sessionFactory" 
                      	class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
                      	
                      	<property name="dataSource"><ref local="dataSource"/></property>
                      	
                      	<!-- Set up the Dependency Injector 
                      	<property name="entityInterceptor"><ref local="dependencyInjectionInterceptor"/></property>
                      	
                      	<property name="mappingResources">
                      		<value>/mycom/packages/mine.hbm.xml</value>
                      	</property>
                      	<property name="hibernateProperties">
                      		<props>
                      			<prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                      			<prop key="hibernate.show_sql">true</prop>
                      		</props>
                      	</property>
                      </bean>
                      
                      
                      
                      <bean id="dependencyInjectionInterceptor"
                      	class="org.springframework.orm.hibernate.support.DependencyInjectionInterceptorFactoryBean"
                      	>
                      	<property name="sessionFactoryName"><value>sessionFactory</value></property>
                      	<property name="defaultAutowireMode"><value>1</value></property>
                      </bean>
                      Note that the configuration of the DependencyInjectionInterceptorFactoryBean here is trivial: please refer to that class's Javadoc for more information about its capabilities. I'll be improving the Javadoc also before it makes the main source tree.

                      this something that will make it into Spring 1.2, or will it remain in the sandbox for some time?
                      It is scheduled for release in 1.2. We should have documentation for it at that time.

                      Comment


                      • #41
                        where can I find the code of org.springframework.orm.hibernate.support.Dependen cyInjectionInterceptorFactoryBean ?

                        Comment


                        • #42
                          In the sandbox directory in CVS.

                          Comment


                          • #43
                            I looked there but could not find it. Could someone give the exact location (or maybe a link)?. Thanks in advance for your help.

                            Comment


                            • #44
                              I found it! I was lokking in the wrong directory. Thnaks for your help.

                              Comment


                              • #45
                                Autowiring an ApplicationContextAware type

                                Originally posted by Rod Johnson
                                The fundamental configuration step is to add an appropriately configured DependencyInjectionInterceptorFactoryBean as a Hibernate interceptor in your Hibernate SessionFactory. Do this via the "entityInterceptor" property of the LocalSessionFactoryBean, for example. ....
                                Everything appears to be being wired up correctly for me using the three new classes from the sandbox with 1.1.1, except that my Hibernate persisted object does not get its setApplicationContext method called after being rehydrated (all the other properties are correctly wired). It implements ApplicationContextAware, but is there something additional that I need to do here?

                                Thanks for any help.

                                Comment

                                Working...
                                X