This tutorial shows how to create a JPA project using a Maven archetype and how to modify it in order to use Hibernate 4 JPA Provider and MySQL.
Maven and JPA tutorial
By using the mvn archetype:generate command you are able to generate a quickstart project from an archetype. Lots of archetypes are available on public repositories and in the next tutorial we will show also how to publish your own archetype.
The simplest way to use the Maven Archetype plugin to generate a new Maven project from an archetype is to run the
archetype:generate goal in interactive mode.
However, once that you have identified one archetype and you are going to use it often in your projects, you can create it in no interactive mode. Here's for example how to create a project which uses the standalone-jpa-eclipselink-archetype (A JPA standalone project which uses Eclipselink JPA provider)
This will generate a sample JPA project in the folder EclipseJPAExample. The project as it is, can be imported into Eclipse using the command:
The project is composed of two domain classes (Employee and Department) and a Test class:
Here's the Department class:
This is the Employee class:
The project by default uses the EclipseLink JPA provider and the Apache Derby embedded database, as stated in the persistence.xml file:
The Test class is named JpaTest, basically insert some sample data in the Employee and Department tables and later on retrieves this data using the listEmployees method.
You can compile/test this class using the following command: