3 Replies Latest reply on Nov 18, 2006 9:20 AM by berserksangr

    Spring-injected ManagedHibernateSession

    berserksangr Newbie

      Hi,
      I have a Spring-wired multilayer application. Is there any way to inject Seam ManagedHibernateSession to my DAO's that are in a separate view-technology-blind layer using a Spring context?
      Seam+Hibernate+Microcontainer example looks like nothing that Spring can't do, but I'm not sure about how it will play together with Seam and Hibernate.

      I like Seam very much, but I don't want to run Microcontainer in my app, when Spring simply gives me what I need. I will be glad to see some examples how to do it.

      Greetings,
      Przemek.


        • 1. Re: Spring-injected ManagedHibernateSession
          berserksangr Newbie

          Can this be achieved by the following?

          1. Configure DataSource and Hibernate session factory using Spring
          2. Bind this session factory to JNDI under java:/sessionFactory
          3. Spawn HibernateManagedSession in components.xml and set sessionFactoryJndiName to java:/bookingSessionFactory
          4. When DAO method is invoked, get HibernateSession using Seam's Contexts.lookupInStatefulContexts(HibernateManagedSession_COMPONENT_NAME).

          TIA

          • 2. Re: Spring-injected ManagedHibernateSession
            Gavin King Master

            You need three things, AFAICT:

            (1) an way to get the sessionfactory into JNDI, so Seam can use it
            (2) a way to inject Seam objects into non-Seam objects
            (3) a JTA environment

            (1) is easy
            (2) is be doable with some spring factories, AFAIK
            (3) is the harder bit (problem when outside-of-Java-EE)

            Unfortunately for everyone, Spring went ahead and created their whole own nonstandard transaction management API that is a subset of JTA (there was no good reason to do this, of course, but people like to reinvent wheels).

            Seam expects a JTA environment (I *don't* like reinventing), and thats what we use JBoss MC for: the JTA and JCA datasource support. With some effort I probably *could* support other transaction APIs than JTA, but why? There is just no good reason to do that, since all transaction APIs that I know of offer less functionality than JTA.

            (People have misinterpreted JBoss MC as being a spring-like project but actually its purpose is quite different. The purpose of MC is to let you run infrastructure components like deployers, jta, jca, jndi, etc in an embeddable way. It not really intended for running business logic components. It is really the core microkernel of JBoss 5.)

            Now, in a Java EE environment, you already have JTA from the container (and, indeed Spring transactions just map down to JTA transactions, duh). So Seam does not need MC in an EE environment. So in the EE environment, (3) is not a problem.

            Outside the EE environment you got a problem, and you need JBoss MC.

            • 3. Re: Spring-injected ManagedHibernateSession
              berserksangr Newbie

              Gavin, thank you for such a quick and useful response. I think I will try with MC.

              Thanks for your hard work,
              Przemek.