Skip to content

Latest commit

 

History

History

setup

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Setting Up a Hibernate Project

Hey guys! I will explain you on how to setup a hibernate project with few examples in Eclipse IDE

Prerequisites

Make sure you have these installed in your system

Step 1: Create a Maven Project in Eclipse

  1. Open Eclipse IDE.
  2. Go to `File -> New -> Maven Project.
mav1 3. Follow the images below mav2 mav3 mav4 4. Click `Finish` to create the project. mavaft

Step 2: Add Dependencies

  1. Open the pom.xml file in your Maven project.
  2. Add the following dependencies for Hibernate Core and MySQL Connector:
pomxml
  1. Save the pom.xml file.

Step 3: Configure Hibernate

  1. In Eclipse, create a Hibernate configuration file (e.g., hibernate.cfg.xml) using JBoss tools(install it in Eclipse Marketplace if not available).
  2. Add the necessary configurations to the file, including the database URL, username, and password.
image

We set the property named "hibernate.show_sql" to true so that whenever we perform some operations on the database using Hibernate it shows the respective sql equivalent command in the output.We also set the another property named "hbm2ddl.auto" so that we don't have to manually create the table in mysql(Hibernate takes care of it)

Step 4: Create the Persistent Class

  1. Create a Java class that represents the entity you want to map to the database table.
  2. Use javax.persistence annotations such as @Entity, @Id, and @Column to define the mapping.
entityclass

Here I have created a class called "student" with three attributes roll(int),name(String),age(int).I have used @Entity before class and @Column before every attributes and also specified their names that they have in sql database . And also I used @Id for the roll attribute because it is a primary key

Step 5: Create Application Code

  1. Create a Java class (e.g., App) to interact with Hibernate.
app

We'll discuss about these things in detail in the future.For now just add these in your App class

Now you should see these results in your db

inserted

Congratulations 🎉

Congrats for creating your first hibernate app!!You did it!!

congrats