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">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.javabydeveloper</groupId>
  <artifactId>JPA-Entity-Retrieve</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>JPA-Entity-Retrieve</name>
  <url>http://maven.apache.org</url>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
  
   <!-- Dependencies for Logging -->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.7</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.7</version>
      </dependency>
      
  <!--     Hibernate and JPA dependencies -->
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.3.10.Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-annotations</artifactId>
          <version>3.5.6-Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>4.3.10.Final</version>
      </dependency>
      
      <!--     Mysql connector Dependency -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.33</version>
      </dependency>
  </dependencies>
</project>

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.

persistence.xml

- Advertisement -

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;
        
        try{
            emf = Persistence.createEntityManagerFactory("jbd-pu");
            entityManager = emf.createEntityManager();
            transaction = entityManager.getTransaction();
            transaction.begin();
            Student student = entityManager.find(Student.class, 1L);
            transaction.commit();
            System.err.println("Student First Name: "+student);
            //System.err.println("Student : "+student.getFirstName());
        }catch(Exception e){
            transaction.rollback();
        }finally{
            entityManager.close();
            emf.close();
        }
    }

- Advertisement -
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. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles