6 Replies Latest reply on Jan 13, 2009 10:03 AM by svadu

    Spring managed entitymanager

    ltackmann

      Hi


      I have a spring application that is used in various contexts (as business logic in a REST application) and now also in a webapp written in Seam.


      Since all the database logic is placed in the spring services I would like to keep it there and simply have Seam using the spring managed entitymanager in its entity-transactions.


      I use Spring 2.5.3 and Seam 2.0.1.GA - I have tried adding the the following to components.xml without luck:


      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/component"
           xmlns:persistence="http://jboss.com/products/seam/persistence"
           xmlns:security="http://jboss.com/products/seam/security"
           xmlns:spring="http://jboss.com/products/seam/spring"
           xmlns:transaction="http://jboss.com/products/seam/transaction"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc"
           xsi:schemaLocation="http://jboss.com/products/seam/components 
           http://jboss.com/products/seam/components-2.0.xsd
           http://jboss.com/products/seam/persistence 
           http://jboss.com/products/seam/persistence-2.0.xsd
           http://jboss.com/products/seam/security 
           http://jboss.com/products/seam/security-2.0.xsd
           http://jboss.com/products/seam/spring 
           http://jboss.com/products/seam/spring-2.1.xsd
           http://jboss.com/products/seam/transaction
           http://jboss.com/products/seam/transaction-2.0.xsd">
      
           <!-- enables our security authenticator -->
           <security:identity
                authenticate-method="#{authenticator.authenticate}" />
      
           <!-- TODO use springs entity manager here -->
           <transaction:entity-transaction entity-manager="#{em}" />
           <persistence:managed-persistence-context name="em"
                auto-create="true" entity-manager-factory="#{entityManagerFactory}" />
      
           <!-- bootstrap spring -->
           <spring:context-loader config-locations="applicationContext.xml" />
      </components>



      hoping that it would use my entityManagerFactory as defined in Springs applicationContext.xml:


      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                   http://www.springframework.org/schema/context 
                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
              http://www.springframework.org/schema/jee 
              http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
                  http://www.springframework.org/schema/tx 
                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
      
           <!-- annotation based dependency injection -->
           <context:component-scan base-package="org.randompage" />
           <context:annotation-config />
      
           <!-- constant injection -->
           <bean id="appName" class="java.lang.String">
                <constructor-arg>
                     <value>SeamWiki</value>
                </constructor-arg>
           </bean>
      
           <!-- enables @AspectJ support -->
           <aop:aspectj-autoproxy />
      
           <!-- enables @PersistenceUnit/@PersistenceContext annotations -->
           <bean
                class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
      
           <!-- data source via JNDI -->
           <jee:jndi-lookup id="dataSource"
                jndi-name="java:comp/env/jdbc/mydb" />
      
           <!-- use persistence.xml for JPA configuration -->
           <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
                <!-- needed for multiple persistence units -->
                <property name="persistenceUnitName" value="default" />
           </bean>
      
           <!-- enables JPA transaction manager -->
           <bean id="transactionManager"
                class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory"
                     ref="entityManagerFactory" />
           </bean>
      
           <!-- enables @Transactional annotation for transaction management -->
           <tx:annotation-driven />
      </beans>



      Can this really not be done ?, since it is pretty common to use the same backend services for multiple endpoints (web, SOAP, REST...) I would really like to find a solution, that does not involve putting Seam into my Spring application (the other way around is desired).


      Thanks in advance.