Page 4 of 6 FirstFirst ... 23456 LastLast
Results 31 to 40 of 58

Thread: How Common to Spring Manage Beans Created By Hibernate?

  1. #31
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  2. #32
    Join Date
    Oct 2004
    Posts
    2

    Default

    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.

  3. #33
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  4. #34
    Join Date
    Oct 2004
    Posts
    4

    Default

    Quote 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

  5. #35
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    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.

  6. #36
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    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

  7. #37
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  8. #38
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    I've fixed this and closed the JIRA issue. The fix uses a name property for the SessionFactory, like Oliver's original contribution.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  9. #39

    Default 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

  10. #40
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Mixing hibernate & spring into single beans
    By ImanRahmati in forum Container
    Replies: 1
    Last Post: Dec 21st, 2004, 07:26 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
  •