Loading…

Hibernate for beginners

I came across (not only me) the problem of running Hibernate. For this reason, I decided to cover this topic. Thus, Hibernate is a popular framework with the goal to link OOP and a relational database. Using Hibernate will reduce project development time in comparison with regular JDBC.

Quite often, beginners face many difficulties in setting up the framework. The help of the development community in highlighting basic challenges will help beginners learn programming languages faster. This article is written only for Java beginners, who first run hibernate. I will show how I deploy hibernate using licensed IDEA.

Here I provide you with Maven framework for automating the project building based on POM. It gives you an opportunity to set dependencies from the Internet without downloading libraries to the project. To be precise, POM (project object model) is a declarative project description. I recommend you to copy the name of the library in XML format from mvnrepository.com.

Initially, you need to create the Maven project structure:

hibernateMaven

 

Now we insert it into pom.xml. Here we will need two dependencies: hibernate-core and mysql-connector. In case you want to have more functionality, you can set more dependencies.

Actually, there are standard guidelines to set dependencies separately, but I do recommend them.

<properties>

        <hibernate-version>5.0.1.Final</hibernate-version>

    </properties>

    <dependencies>

        <!--driver for connection to MYSql database -->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.34</version>

        </dependency>



        <!-- Hibernate -->

        <!-- to start need only this -->

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-core</artifactId>

            <version>${hibernate-version}</version>

        </dependency>



        <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-entitymanager</artifactId>

            <version>${hibernate-version}</version>

        </dependency>

    </dependencies>

 

Then, you need to click Import Changes Enable Auto-Import button to import the changes.

image

Now we connect to a database that is run on a local machine. I recommend to choose MySQL as a database provider.

 

image

After, you need to enter the database name, login, and password and test the connection.

image

Choose the project and ask framework support to create Entity files and Getter & Setter classes.

image

image

Now you need to open the Persistence tab and select Generate Persistence Mapping. In the appeared window set the database scheme: select prefix and suffix for the automatically generated names. Thus, you will get generated XML files and classes with the following annotations:

image

Put the files in the following way: .xml files should be stored in the same directory with the resources, and the entities in java directory.

image

Now, you need to write up the username and password in hibernate.cfg.

image

There you are. Hereafter you can run the project through class main.

This is my first article. Waiting for the constructive feedback.


Leave a Comment