JPA we cannot use independently means we need ORM tool to implement the JPA
Spring-ORM module
This module helps to integrated JPA+ORM with Spring to make our job easy...
Two options:-
1. Spring +Hibernate 3.x
2. Spring +JPA+Hibernate 3.x
it makes your application ORM independent
JPA is standard which was introduce JEE5 JPA 1.0
Step-1
Adding Dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Hibernate 3.x +JPA Dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.3.Final</version>
</dependency>
Step-2
Create EntityManagerFactoryBean inside the meta configuration(spring-jpa-hibernate.xml)
<bean id="strutsJPAEntityFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jpadataSource" />
<property name="packagesToScan" value="com.struts.mvc.dao.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
Step-3
<bean id="jpadataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/profile_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
Step-4
<!-- This is because spring will do transaction management with jpa-->
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="strutsJPAEntityFactory" />
</bean>
Step-5
<!--Below entry is associating jpaTransactionManager with @Transactional annotation -->
<tx:annotation-driven transaction-manager="jpaTransactionManager" proxy-target-class="false"/>
</beans>
Note : without spring transaction we cannot use Spring and JPA integration
Step-6
@Repository("UserDao")
@Scope("singleton")
@Transactional
public class UserDao implements IUserDao{
//LocalContainerEntityManagerFactoryBean
//@PersistenceContext pulls an EntityManager from LocalContainerEntityManagerFactoryBean bean manage by spring
//container for JPA
@PersistenceContext
private EntityManager entityManager;
@Override
public String persistUser(UserEntity userEntity) {
boolean enabled=TransactionSynchronizationManager.isActualTransactionActive();
if(enabled) {
System.out.println("@_@_@__@_@_@YEAP We are using_________*@@@@@@@@@@@@@@");
}
System.out.println("||||||||||||||||||||||||");
System.out.println(userEntity);
entityManager.persist(userEntity);
System.out.println("||||||||||||||||||||||||");
return "yeap";
}
No comments:
Post a Comment