Hibernate Setup in eclipse with Maven and MySql DB

Let us see Hibernate Installation and Setup on Eclipse with Maven and MySql database


Tools and Technologies used

Java

Eclipse

Maven

Hibernate

MySql database


Note: If you are not using Maven then you must download all the Hibernate related jars from below link
http://hibernate.org/ and add them in project build path.

Step 1

Right click on project explorer and select New Project

SpringHibernateSetup_

Select maven project and click on Next

SpringhibernatecreateProj2

Click on Next and select maven web app archetype

SpringHibernatecreateProj3

Enter archetype parameters as below and click on Finish

hibernate_setup_1

Step 2

If there is any error icon in the project, add the servlet-api maven dependency into pom file.

  1. <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
  2. <dependency>
  3.     <groupId>javax.servlet</groupId>
  4.     <artifactId>servlet-api</artifactId>
  5.     <version>2.5</version>
  6. </dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>


Now the Project Structure in eclipse should be

hibernate_eclipse_setup_2

Step 3

Add the Hibernate dependencies to pom file as below

  1. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  2. <dependency>
  3.     <groupId>org.hibernate</groupId>
  4.     <artifactId>hibernate-core</artifactId>
  5.     <version>5.2.6.Final</version>
  6. </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.6.Final</version>
</dependency>


Add the database connector dependency

I am using MySql Database, so adding corresponding dependency

  1. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  2. <dependency>
  3.     <groupId>mysql</groupId>
  4.     <artifactId>mysql-connector-java</artifactId>
  5.     <version>6.0.5</version>
  6. </dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.5</version>
</dependency>


Complete pom.xml file is as below

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>HibernateSetup</groupId>
  5.   <artifactId>HibernateSetup</artifactId>
  6.   <packaging>war</packaging>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <name>HibernateSetup Maven Webapp</name>
  9.   <url>http://maven.apache.org</url>
  10.   <dependencies>
  11.     <dependency>
  12.       <groupId>junit</groupId>
  13.       <artifactId>junit</artifactId>
  14.       <version>3.8.1</version>
  15.       <scope>test</scope>
  16.     </dependency>
  17.     <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
  18. <dependency>
  19.     <groupId>javax.servlet</groupId>
  20.     <artifactId>servlet-api</artifactId>
  21.     <version>2.5</version>
  22. </dependency>
  23.  
  24. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  25. <dependency>
  26.     <groupId>org.hibernate</groupId>
  27.     <artifactId>hibernate-core</artifactId>
  28.     <version>5.2.6.Final</version>
  29. </dependency>
  30. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  31. <dependency>
  32.     <groupId>mysql</groupId>
  33.     <artifactId>mysql-connector-java</artifactId>
  34.     <version>6.0.5</version>
  35. </dependency>
  36.    
  37.   </dependencies>
  38.   <build>
  39.     <finalName>HibernateSetup</finalName>
  40.   </build>
  41. </project>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>HibernateSetup</groupId>
  <artifactId>HibernateSetup</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HibernateSetup Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.5</version>
</dependency>
    
  </dependencies>
  <build>
    <finalName>HibernateSetup</finalName>
  </build>
</project>


Now Application is ready to use Hibernate

Step 4

Setup the MySql databse in local system for practice

Access the below link and download the MySql installer
https://dev.mysql.com/downloads/installer/

mysql_installer_download_1

click on download

mysql_installer_download_2

Install the MySql installer

mysql_installer_install

mysql_installer_install_2

mysql_installer_install_3

mysql_installer_install_4

mysql_installer_install_5

click on next to finish the installation

mysql_installer_install_6

click on finish

Step 5

Test Installation

Open command prompt
Go to the MySql installation path (In my case it is E:\MySql_Install\bin)

Type mysql –u root –p

Provide the password given above while installing mysql

mysql_command_line1

mysql_command_line2

Now MySql database is installed

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on