I am trying to integrate our DAO maven module in spring, we do not want to change any dao code. Previously, all dao classes get an entity manager as follows.
Persistence.getEntityManager()
In spring, I see many examples that use annotation to enter entity manager, but need to change the dao code. Is there a way to change the class below to make it work in spring?
public class PersistenceManager { private static final String ENTITY_MANAGER_INSTANCE = "ENTITY_MANAGER_INSTANCE"; private static final String PERSISTENCE_UNIT_EMYED = "application_openjpa"; private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence .createEntityManagerFactory(PERSISTENCE_UNIT_EMYED); public static void close() { EntityManager emInstance = getEntityManager(); try { if (emInstance.isOpen()) { emInstance.close(); } } catch (Throwable t) {
Spring Configuration
<description>Example configuration to get you started.</description> <context:component-scan base-package="com.veera" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"> <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary" /> </bean> </property> <property name="persistenceUnitName" value="application_openjpa" /> </bean> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
persistence.xml
<persistence-unit name="application_openjpa" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>com.veera.jpa.Item</class> <class>com.veera.jpa.Order</class> <class>com.zreflect.emyed.entity.user.User</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="openjpa.Log" value="File=stdout, DefaultLevel=INFO, Runtime=INFO, SQL=TRACE"/> </properties> </persistence-unit> </persistence>
source share