Create a Maven JPA Project


These instructions are for Eclipse or Spring Tool Suite (STS)

1. Make Certain you have a Database Connection

  • Window -> Show View -> Other -> Database Management -> Database Explorer
  • Database Connections -> New -> PostgreSQL -> Next
  • New Driver Definition -> Jar List -> Add Jar Zip
  • select your downloaded PostgreSQL JDBC Jar
  • OK
  • set  Database, URL, Username, and password
  • Test Connection (make certain your database is running)
  • Finish

2. Create a Java Project

  • New -> Other -> Maven -> Maven Project -> Next
  • Check ‘Create a Simple Project (skip archetype selection)
  • Next
  • Enter Group Id – e.g. com.skills421.jpa
  • Enter Artifact Id – e.g SimpleJPA
  • Finish

3. Edit the Pom.xml

3.1 Add the eclipselink Maven pom entries

  • google eclipselink maven
  • http://wiki.eclipse.org/EclipseLink/Maven
  • Grab the Repository and Dependencies Pom entries and add them to your Pom.xml
  • Replace {artifactid} with the appropriate artifactid – e.g. eclipselink
  • Replace {version} with the current version – e.g 2.4.0
  • Save the Pom.xml and you should see some jar files appear in Maven Dependencies
  • Add the dependencies for EclipseLink by clicking on the link next to the artifact on the web page and adding the dependencies from the sample pom.xml

Save the Pom.xml and you should see some jar files appear in Maven Dependencies

3.2 Add the JDBC Driver

Save the Pom.xml and you should see some jar files appear in Maven Dependencies

4. Configure the EclipseLink JPA

  • Project -> Properties -> Project Facets -> Convert to Faceted Form
  • Check JPA
  • Click Further Configuration Required
  • Platform -> Select Eclipselink Version – e.g. EclipseLink 2.4.x
  • JPA Implementation -> Disable Library Configuration
  • Connection -> PostgreSQL Connection (as created in 1. above)
  • OK
  • OK

This will create META-INF/persistence.xml

5. Configure the Persistence.xml

  • Open Persistence.xml
  • Click Connection tab
  • Transaction Type -> Resource Local
  • EclipseLink Connection Pool -> Populate from Connection
  • Select your Connection (as created in 1. above)
  • OK

** END **

The Project is now fully configured for work with JPA

You Pom.xml will look something like this

<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>com.skills421.jpa</groupId>
<artifactId>mavenjpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- EclipseLink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.4.v201112161009</version>
<type>jar</type>
<scope>compile</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>commonj.sdo</groupId>
<artifactId>commonj.sdo</artifactId>
<version>2.1.1.v201112051852</version>
<type>jar</type>
<scope>compile</scope>
<optional>false</optional>
</dependency>

<!-- PostgreSQL -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>

</dependencies>

<repositories>
<repository>
<id>EclipseLink</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
</project>
Advertisement

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s