Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

convert to JPA

  • Loading branch information...
commit c09e5ee86086f9b555e2554544e33061590bcff6 1 parent 1929285
John Simone jsimone authored
2  .gitignore
View
@@ -2,6 +2,6 @@
/.classpath
/.project
/.settings
-/tomcat.8080
+/tomcat.*
/.idea
/*.iml
19 src/main/java/heroku/template/service/PersonServiceImpl.java
View
@@ -4,34 +4,33 @@
import java.util.List;
-import org.hibernate.SessionFactory;
-import org.springframework.beans.factory.annotation.Autowired;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class PersonServiceImpl implements PersonService {
- @Autowired
- private SessionFactory sessionFactory;
+ @PersistenceContext
+ EntityManager em;
@Transactional
public void addPerson(Person person) {
- sessionFactory.getCurrentSession().save(person);
+ em.persist(person);
}
@Transactional
public List<Person> listPeople() {
-
- return sessionFactory.getCurrentSession().createQuery("from Person").list();
+ return em.createQuery("from Person").getResultList();
}
@Transactional
public void removePerson(Integer id) {
- Person person = (Person) sessionFactory.getCurrentSession().load(
- Person.class, id);
+ Person person = (Person) em.find(Person.class, id);
if (null != person) {
- sessionFactory.getCurrentSession().delete(person);
+ em.remove(person);
}
}
8 src/main/resources/META-INF/persistence.xml
View
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
+
+ <persistence-unit name="dataSource" transaction-type="RESOURCE_LOCAL">
+ </persistence-unit>
+</persistence>
51 src/main/resources/applicationContext.xml
View
@@ -19,24 +19,26 @@
</bean>
<tx:annotation-driven />
-
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
+ <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ <property name="dataSource" ref="dataSource"/>
+
+ </bean>
<beans profile="default">
- <jdbc:embedded-database id="dataSource"/>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="packagesToScan" value="heroku.template.model"/>
- <property name="hibernateProperties">
- <props>
+ <jdbc:embedded-database id="dataSource"/>
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
+ <property name="dataSource" ref="dataSource"/>
+ <property name="jpaVendorAdapter">
+ <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
+ </property>
+ <property name="jpaProperties">
+ <props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
- </props>
- </property>
- </bean>
+ </props>
+ </property>
+ </bean>
</beans>
<beans profile="prod">
@@ -49,18 +51,21 @@
<property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
</bean>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="packagesToScan" value="heroku.template.model"/>
- <property name="hibernateProperties">
- <props>
+
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
+ <property name="dataSource" ref="dataSource"/>
+ <property name="jpaVendorAdapter">
+ <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
+ </property>
+ <property name="jpaProperties">
+ <props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
+ <!-- change this to 'verify' before running as a production app -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
+ </props>
+ </property>
+ </bean>
</beans>
</beans>
Please sign in to comment.
Something went wrong with that request. Please try again.