I use Maven for building so these were the required dependencies:
- <dependency>
- <groupid>org.eclipse.persistence</groupid>
- <artifactid>javax.persistence</artifactid>
- <version>2.0.0</version>
- </dependency>
- <dependency>
- <groupid>org.aspectj</groupid>
- <artifactid>aspectjrt</artifactid>
- <version>1.6.9</version>
- </dependency>
- <dependency>
- <groupid>org.springframework</groupid>
- <artifactid>spring-aspects</artifactid>
- <version>3.0.4.RELEASE</version>
- </dependency>
Here is Spring's application context file:
- <!--xml version="1.0" encoding="UTF-8"?-->
- <beans xmlns="http://www.springframework.org/schema/beans" <brbr=""> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
- <context:annotation-config>
- <context:component-scan base-package="your.services.package">
- <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
- <tx:annotation-driven transaction-manager="txManager">
- <aop:spring-configured>
- <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="emf">
- </property></bean>
- <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
- <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform">
- <property name="showSql" value="true">
- </property></property></bean>
- <bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
- <property name="persistenceUnitName" value="your_persistence_unit_name">
- <property name="jpaVendorAdapter" ref="jpaAdapter">
- </property></property></bean>
- </aop:spring-configured></tx:annotation-driven></bean></context:component-scan></context:annotation-config></beans>
Now inside of your.services.package you can have such class:
- @Component //instantiate as Spring's bean
- public class ProductServiceImpl implements ProductService {
- //Trick part #1 - Spring substitutes here entity manager
- //and handles it in a way that each thread thinks he's the only one working with this manager.
- //For E.M. marked by this annotation declarative transaction management is possible
- @PersistenceContext(type = PersistenceContextType.TRANSACTION)
- private EntityManager containerEm;
- //Trick part #2 - Spring wraps this method in transaction which is handled by containerEm
- @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
- public void yourMethod() {
- ...
- //Here you can do whatever you want with E.M.:
- containerEm.createNativeQuery(...);
- //or
- em.persist(...);
- //etc.
- ...
- }//end of transactional method
- }
1 comment:
Are you sure that this configuration will work? i got the following error:
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'tx:annotation-driven' must have no character or element information item [children], because the type's content type is empty.
Post a Comment