Advertisement



< Prev
Next >



Hibernate - The simple Annotations



Today we are going to understand how to use simple Hibernate annotations with our Model class(POJO class), which needs to be mapped to a database table and whose objects need to be persisted in the database using Hibernate.

As many of you must know that Hibernate is a middleware used for object-relational mapping(ORM) and for performing efficient object persistence.

In this article, we are going to create a Java class which will be mapped to a table in our database(Oracle Express Edition 10g) and its objects will be persisted, using the Hibernate.




Note :


In the upcoming example, We are going to create a Java class which will be mapped to a table in our database(Oracle Express Edition 10g). We are going to use some Hibernate Annotations, hence, there will be no need to create the mapping-resource with extension hbm.xml . And yes, one more thing, we are going to create this program in an Integrated Development Environment(IDE) , so you use any IDE of your choice.




The simple Hibernate Annotations




Note : Annotations are added on the top of an instance variable/property in a java class with which it is going to be associated or on the top of its getter method.








Adding annotations to a POJO/Entity class







Execution


Finally, after executing Hiber class, you will get the following output within the Console window of your IDE or Command Prompt, as shown below.

Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table USER_INFO (ID number(10,0) not null, databaseCreatedOn date, First_Name varchar2(255 char), Last_Name varchar2(255 char), primary key (ID))
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into USER_INFO (databaseCreatedOn, First_Name, Last_Name, ID) values (?, ?, ?, ?)
Hibernate: insert into USER_INFO (databaseCreatedOn, First_Name, Last_Name, ID) values (?, ?, ?, ?)
Hibernate: insert into USER_INFO (databaseCreatedOn, First_Name, Last_Name, ID) values (?, ?, ?, ?)
Hibernate: select userinfo0_.ID as ID1_0_0_, userinfo0_.databaseCreatedOn as databaseCreatedOn2_0_0_, userinfo0_.First_Name as First_Name3_0_0_, userinfo0_.Last_Name as Last_Name4_0_0_ from USER_INFO userinfo0_ where userinfo0_.ID=?
Hibernate: select userinfo0_.ID as ID1_0_0_, userinfo0_.databaseCreatedOn as databaseCreatedOn2_0_0_, userinfo0_.First_Name as First_Name3_0_0_, userinfo0_.Last_Name as Last_Name4_0_0_ from USER_INFO userinfo0_ where userinfo0_.ID=?
Hibernate: select userinfo0_.ID as ID1_0_0_, userinfo0_.databaseCreatedOn as databaseCreatedOn2_0_0_, userinfo0_.First_Name as First_Name3_0_0_, userinfo0_.Last_Name as Last_Name4_0_0_ from USER_INFO userinfo0_ where userinfo0_.ID=?
Retrieving the saved objects
First User
Id : 1
First Name : Paul
Last Name : Walker
Datebase created : 2018-05-18
Second User
Id : 2
First Name : Leonardo
Last Name : Dicaprio
Datebase created : 2018-05-18
Third User
Id : 3
First Name : Johnny
Last Name : Depp
Datebase created : 2018-05-18


This output shows you that Hibernate has used its sequence object to generate the value of each primary key automatically plus all the SQL commands executed by the Hibernate within the database to map the Java class to a database table and perform all the activities of saving, modifying, deleting and retrieving the objects of UserInfo.




Please share this article -




< Prev
Next >
< Hibernate program in IDE
Hibernate Value Type Objects >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement