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


    • #32
      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


      • #33
        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


        • #34
          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


          • #35
            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


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

              Comment


              • #37
                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


                • #38
                  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


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

                    Comment


                    • #40
                      In the sandbox directory in CVS.

                      Comment


                      • #41
                        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


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

                          Comment


                          • #43
                            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


                            • #44
                              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 won't call setApplicationContext() with the autowiring approach. Autowiring does not register the object with the application context as a prototype, it merely does Dependency Injection based on resolving properties. This is consistent with the BeanFactory semantics on autowiring existing objects with dependencies from a factory.

                              If you want the ApplicationContextAware and other lifecycle interfaces to be invoked, you should use full factory management of your beans by defining them as prototypes.

                              Guess I should update the Javadoc to indicate this.

                              Comment


                              • #45
                                thanks

                                Originally posted by Rod Johnson
                                If you want the ApplicationContextAware and other lifecycle interfaces to be invoked, you should use full factory management of your beans by defining them as prototypes.
                                Ah, that makes sense now. After changing autowire to 0 and defining the prototypes in managedClassNamesToPrototypeNames, it works great.

                                Thanks...

                                Comment

                                Working...
                                X