Java Hibernate – Example

Java Hibernate – Example 2017-08-14T14:02:11+00:00

Java Hibernate Example

Write a java hibernate example or program to insert a record in database.

NOTE: The all theoretical steps, source code and program to insert a record in database is described below and the steps you should perform in MyEclipse to do java hibernate example is available in above video.
Step 1: Right Click on project and select My Eclipse and then select the Add Hibernate Capabilities.
Step 2: Now Click on Next Button.
Step 3: Again Click on Next Button.
Step 4: Select the appropriate database at DB Driver combo box and then click on Next button.
Step 5: Select the appropriate Java package by click on Browse button.
Step 6: Then click on OK button.
From here now the programming files will be generated and still total 8 steps are remaining that is mentioned below this tutorial is especially created for beginners to do java hibernate example.
Step 7: Then click on Finish button So it will automatically create two files:
1. This is an xml file(hibernate configuration) which is available in the src like hibernate.cfg.xml which contains the properties of connection with database for session-factory like username, url, dialect, profile,  password, driver_class.
2. Second is a file which is available at the com package for handling the session and its properties.
Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment.
1. hibernate.cfg.xml:

Hibernate supports many database.
With the use of the Hibernate (Object/Relational Mapping and Transparent Object Persistence for Java and SQL Databases), we can use the following databases dialect type property:
DB2 – org.hibernate.dialect.DB2 Dialect
Oracle (any version) – org.hibernate.dialect.OracleDialect
Oracle 9 – org.hibernate.dialect.Oracle9Dialect
MySQL – org.hibernate.dialect.MySQLDialect
PostgreSQL – org.hibernate.dialect.PostgreSQLDialect
Sybase – org.hibernate.dialect.SybaseDialect
Sybase Anywhere – org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server – org.hibernate.dialect.SQLServerDialect
HypersonicSQL – org.hibernate.dialect.HSQ LDialect
Informix – org.hibernate.dialect.InformixDialect
Ingres – org.hibernate.dialect.IngresDialect
Interbase – org.hibernate.dialect.InterbaseDialect
Pointbase – org.hibernate.dialect.PointbaseDialect
Mckoi SQL – org.hibernate.dialect.MckoiDialect
Progress – org.hibernate.dialect.ProgressDialect
FrontBase – org.hibernate.dialect.FrontbaseDialect
SAP DB – org.hibernate.dialect.SAPDBDialect

Step 8: Now add resource in mapping tag for the mapping with an appropriate table in hibernate.cfg.xml file.
<mapping resource=” contact.hbm.xml “/>
It is for mapping for our contact table.

Step 9: Now Create the First Persistence Class(java file).
Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table so We can configure the variables to map to the database column.

Step 10: Create the contact.hbm.xml file in a default package.

Step 11: Now create the java file by which the data is inserting in the table (CONTACT) at database.

Hibernate Session is the main runtime interface between a Java application and Hibernate. First we are required to get the Hibernate Session.SessionFactory allows application to create the Hibernate Sesssion by reading the configuration from hibernate.cfg.xml file.
Then the save method on session object is used to save the contact information to the database:
Step 12: Select a MyEclipse Hibernet view and creating a table (CONTACT) in APP at MyEclipse Derby.
Step 13: Now Start the Database, Then check the message in Console.
Apache Derby Network Server – – (485682) started and ready to accept connections on port 1527 at 2012-12-29 09:34:17.187 GMT
Step 14: After getting such message in Console, Run the file as a java application and following output will display in the Console.
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record…………
Enter Id :
Enter First name :
Enter Last name :
Learn Java Online
Enter Email id :
Data inserted successfully………

This is a simple java hibernate example to insert a record in database.

Prev Next