Step by Step Hibernate - Your First Hibernate Application
By: Felix in Hibernate Tutorials on 2023-05-08
Here are the steps to create a simple Hibernate application:
-
Create a new Java project in your IDE of choice, and add the Hibernate libraries to your classpath. You can download the latest version of Hibernate from the official website.
-
Create a new Hibernate configuration file (
hibernate.cfg.xml
) in the root of your project, and set up the database connection details. For example:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
Replace the database connection details with your own.
- Create a new Java class to represent a table in your database. This class should have a no-argument constructor and getter and setter methods for each property. For example:
@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username") private String username; @Column(name = "email") private String email; // no-argument constructor public User() {} // getter and setter methods public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
- Create a new Hibernate session factory object, using the configuration file you created in step 2. For example:
Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory sessionFactory = cfg.buildSessionFactory();
- Open a new Hibernate session using the session factory. For example:
Session session = sessionFactory.openSession();
- Create a new transaction using the session. For example:
Transaction tx = session.beginTransaction();
- Create a new instance of the User class you created in step 3, and set its properties. For example:
User user = new User(); user.setUsername("john_doe"); user.setEmail("[email protected]");
- Save the new object to the database using the session. For example:
session.save(user);
- Commit the transaction using the session. For example:
tx.commit();
- Close the session using the session factory. For example:
sessionFactory.close();
That's it! You've created a simple Hibernate application that saves a new user object to the database. You can use similar steps to retrieve, update, and delete objects from the database using Hibernate.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Step by Step Hibernate - Your First Hibernate Application
A sample Hibernate Web Application using Servlets
Programmatic configuration in Hibernate
Tutorial Using the Java Persistence API (JPA) in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate
Identity columns and sequences in Hibernate
EntityNameResolvers in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
equals() and hashCode() in Hibernate
Fetching strategies in Hibernate
Comments