Home Hibernate JPA + Mysql + Maven example setup in eclipse

JPA + Mysql + Maven example setup in eclipse

Project Structure :

Jpa mysql maven project setup import maven in eclipse

pom.xml (Project Object Model):

  • It is an XML file that contains information about the project and configuration details used by Maven to build the project.
  • We will define required jars or wars or libraries as dependencies. Below is the pom.xml looks like.
  • All the required JPA and Hibernate dependencies will be defined here.
<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">
   <!-- Dependencies for Logging -->
  <!--     Hibernate and JPA dependencies -->
      <!--     Mysql connector Dependency -->

Update maven dependencies after project imported :

  • Right click on project –> select “maven” –> select “Update Project”. Follow below screen.
  • Select “Force Update…” check box –> click “OK”. This will update all dependencies to the project.

Import jpa mysql maven project into eclipse

Run SQL script that provided in sample application:

  • Every download of sample application includes sql file also.
  • Copy provided sql script and execute using MySql workbench. Follow below screen.
  • Paste sql code in the editor –> select part of script or whole script that you want to execute –> issue command CTRL+SHIFT+ENTER (in windows, CMD+SHIFT+ENTER in Mac).

import mysql sript into workbench

Persistence.xml :

  • Every sample application includes persistence.xml inside META-INF directory.
  • JPA uses the persistence.xml file to create the connection and setup the required environment. Provides information which is necessary for making database connections.
    • Persistence units are defined by the persistence.xml configuration file.
    • A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application.


App.java – main class to test the code :

  • All the entities are available under “domain” source directory.
  • You will find App.java, use that class to run the test.
public static void main( String[] args )
        EntityManagerFactory emf = null;
        EntityManager entityManager = null;
        EntityTransaction transaction = null;
            emf = Persistence.createEntityManagerFactory("jbd-pu");
            entityManager = emf.createEntityManager();
            transaction = entityManager.getTransaction();
            Student student = entityManager.find(Student.class, 1L);
            System.err.println("Student First Name: "+student);
            //System.err.println("Student : "+student.getFirstName());
        }catch(Exception e){

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook


Please enter your comment!
Please enter your name here

Stay in Touch