Hey guys! I will explain you on how to setup a hibernate project with few examples in Eclipse IDE
Make sure you have these installed in your system
- Open Eclipse IDE.
- Go to `File -> New -> Maven Project.
- Open the
pom.xml
file in your Maven project. - Add the following dependencies for Hibernate Core and MySQL Connector:
- Save the
pom.xml
file.
- In Eclipse, create a Hibernate configuration file (e.g.,
hibernate.cfg.xml
) using JBoss tools(install it in Eclipse Marketplace if not available). - Add the necessary configurations to the file, including the database URL, username, and password.
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)
- Create a Java class that represents the entity you want to map to the database table.
- Use
javax.persistence
annotations such as@Entity
,@Id
, and@Column
to define the mapping.
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
- Create a Java class (e.g.,
App
) to interact with Hibernate.
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
Congrats for creating your first hibernate app!!You did it!!