Integration of Spring and JPA

1)Create Persistence and place in META-INF folder

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
                                 http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="tscUnit1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.tutorialspoint.models.Stock</class>
<class>com.tutorialspoint.models.StockDailyRecord</class>
<exclude-unlisted-classes />
<properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>


2)Declaration of Spring-context.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.tutorialspoint.controller" />
<tx:annotation-driven />
<context:annotation-config />
<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/mkyongdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="emf1"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--data source injection -->
<property name="dataSource" ref="dataSource" />
<!-- persistence unit name injection -->
<property name="persistenceUnitName" value="tscUnit1" />
<property name="persistenceXmlLocation" value="classpath:META-INF/spring-persistence1.xml" />
</bean>

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="unit1" value="tscUnit1" />
</map>
</property>
</bean>

<bean id="tx1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf1" />
</bean>

<bean id="stockDaoImpl" class="com.virtusa.generic.dao.GenericDaoImpl">
<constructor-arg value="com.tutorialspoint.models.Stock" />
</bean>

</beans>



Comments