Created
February 29, 2012 22:58
-
-
Save gdenning/1945181 to your computer and use it in GitHub Desktop.
How to configure OpenJPA using Spring 3 with Annotation-Based Transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
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"> | |
<context:component-scan base-package="com.example" /> | |
<context:annotation-config/> | |
<!-- ******************************************************************** --> | |
<!-- Include context files from different layers --> | |
<!-- ******************************************************************** --> | |
<import resource="security-context.xml" /> | |
<import resource="service-context.xml" /> | |
<import resource="dao-context.xml" /> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.dao.impl | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Query; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.stereotype.Component; | |
@Component("CustomerDao") | |
public class CustomerDaoImpl { | |
@PersistenceContext | |
private EntityManager em; | |
public Customer getCustomerByName(String customerName) { | |
Query query = getEm().createQuery("SELECT c FROM Customer c WHERE c.name = ?1"); | |
query.setParameter(1, customerName); | |
List<Customer> results = query.getResultList(); | |
if (results.size() > 0) { | |
return results.get(0); | |
} | |
return null; | |
} | |
protected EntityManager getEm() { | |
return em; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns="http://www.springframework.org/schema/beans" | |
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/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> | |
<!-- ******************************************************************** --> | |
<!-- Mark bean transactions as annotation driven --> | |
<!-- ******************************************************************** --> | |
<tx:annotation-driven transaction-manager="transactionManager" /> | |
<!-- ******************************************************************** --> | |
<!-- PropertyConfigurer for the DAO --> | |
<!-- ******************************************************************** --> | |
<bean id="propertyConfigurer" | |
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> | |
<property name="location" value="classpath:db.properties" /> | |
</bean> | |
<!-- ******************************************************************** --> | |
<!-- Setup the transaction manager --> | |
<!-- ******************************************************************** --> | |
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | |
<property name="entityManagerFactory" ref="entityManagerFactory" /> | |
</bean> | |
<!-- ******************************************************************** --> | |
<!-- Setup each data source --> | |
<!-- ******************************************************************** --> | |
<bean id="dataSource" | |
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | |
<property name="driverClassName" value="${connection.driver_class}" /> | |
<property name="url" value="${connection.url}" /> | |
<property name="username" value="${connection.username}" /> | |
<property name="password" value="${connection.password}" /> | |
</bean> | |
<!-- ******************************************************************** --> | |
<!-- Setup each persistence unit --> | |
<!-- ******************************************************************** --> | |
<!-- Configure a JPA vendor adapter --> | |
<bean id="openJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"> | |
<property name="showSql" value="${connection.show_sql}" /> | |
<property name="generateDdl" value="${connection.generateDdl}" /> | |
<property name="databasePlatform" value="${connection.dialect}" /> | |
</bean> | |
<!-- EntityManager Factory that brings together the persistence unit, datasource, | |
and JPA Vendor --> | |
<bean id="entityManagerFactory" | |
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
<property name="dataSource" ref="dataSource" /> | |
<property name="jpaVendorAdapter" ref="openJpaVendorAdapter" /> | |
<property name="persistenceUnitName" value="scrum-board-persistence-unit" /> | |
</bean> | |
<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> | |
<property name="entityManagerFactory" ref="entityManagerFactory"/> | |
</bean> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" | |
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_2_0.xsd"> | |
<persistence-unit name="entityManagerFactory" transaction-type="RESOURCE_LOCAL" > | |
<description>generated-persistence-unit</description> | |
<provider>org.apache.renamed.openjpa.persistence.PersistenceProviderImpl</provider> | |
<class>com.example.Class1</class> | |
<class>com.example.Class2</class> | |
</persistence-unit> | |
</persistence> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>group.name</groupId> | |
<artifactId>artifact</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<name>Project Name</name> | |
<dependencies> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<scope>provided</scope> | |
<version>3.0.1</version> | |
</dependency> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
<version>1.2.16</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
<version>3.0.5.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>3.0.5.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>3.0.5.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>3.0.5.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.openjpa</groupId> | |
<artifactId>openjpa</artifactId> | |
<version>2.2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.jdbc</groupId> | |
<artifactId>ojdbc5</artifactId> | |
<version>11.2.0.1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.persistence</groupId> | |
<artifactId>javax.persistence</artifactId> | |
<version>2.0.0</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>openjpa-maven-plugin</artifactId> | |
<version>1.2</version> | |
<configuration> | |
<connectionDriverName>oracle.jdbc.driver.OracleDriver</connectionDriverName> | |
<includes>${basedir}/target/classes/**/*.class</includes> | |
<addDefaultConstructor>true</addDefaultConstructor> | |
<enforcePropertyRestrictions>true</enforcePropertyRestrictions> | |
</configuration> | |
<executions> | |
<execution> | |
<id>enhancer</id> | |
<phase>process-classes</phase> | |
<goals> | |
<goal>enhance</goal> | |
</goals> | |
</execution> | |
</executions> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.openjpa</groupId> | |
<artifactId>openjpa</artifactId> | |
<version>2.2.0</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
could you please share your db.properties file? thanks
What should db.properties look like?
db.properties should just hold the constants regarding to DB. it should depend on your own DB setup.
<!-- ******************************************************************** -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${connection.driver_class}" />
<property name="url" value="${connection.url}" />
<property name="username" value="${connection.username}" />
<property name="password" value="${connection.password}" />
</bean>
<!-- ******************************************************************** -->
<!-- Setup each persistence unit -->
<!-- ******************************************************************** -->
<!-- Configure a JPA vendor adapter -->
<bean id="openJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="showSql" value="${connection.show_sql}" />
<property name="generateDdl" value="${connection.generateDdl}" />
<property name="databasePlatform" value="${connection.dialect}" />
</bean>
these properties should come from there
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could you please share your db.properties file