
Play Store Application link – Hibernate – in 10 steps – Apps on Google Play
Configuring Hibernate is the process of setting up the framework to connect to a database, define mapping files and specify the persistent objects that will be used in the application.
There are following steps to perform-
1- Importing Hibernate libraries (Classes/Interfaces)
2- Creating Hibernate Configuration File
Step 1-
- Add Hibernate libraries to your project
- Libraries can be found on the Hibernate website
- Libraries needed: core Hibernate framework, Hibernate annotations, and database driver
Here is an example of how to add the necessary libraries to a project using Maven:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
-> Group ID (groupId
): It’s like a company or organization name that groups related projects together. Example: org.hibernate
means it belongs to the Hibernate organization.
->Artifact ID (artifactId
): It’s the specific project or library name within that group. Example: hibernate-core
means it is the core Hibernate library.
Note – There are several alternative ways to import dependencies without using a pom.xml file:
- You can manually download the JAR files for the dependencies and add them to the classpath of your project.
- You can use the Gradle or Apache Ivy build tools, which do not require a pom.xml file. Instead, you can specify dependencies in a build.gradle or ivy.xml file.
- You can use a package manager such as Maven Wrapper, SDKMAN or Jabba that can download the dependencies and manage them for you, without the need for a pom.xml file.
- You can use a build tool like Apache Ant which uses build.xml file instead of pom.xml to manage dependencies.
- Some IDEs like Eclipse and IntelliJ IDEA also have support for adding dependencies to a project without using a pom.xml file.
Important-
Hibernate Interfaces/Class | Description |
---|---|
Configuration class | Used to configure Hibernate settings and create a SessionFactory |
Session Interface | Represents a single unit of work with the database, used for creating, reading, updating, and deleting objects, and committing or rolling back a transaction |
SessionFactory Interface | Factory for creating Session objects, thread-safe and can be used to create multiple sessions |
Transaction Interface | Represents a unit of work with the database, used to group multiple operations together and commit or rollback all of them at once |
Query Interface | Used to execute HQL (Hibernate Query Language) and native SQL queries, can be used to retrieve, update, or delete data |
Criteria Interface | Used to create type-safe and type-sensitive queries, can be used to retrieve data based on certain criteria, retrieve specific fields, or retrieve data based on certain conditions |
Step 2- Next,
- Create a configuration file named “hibernate.cfg.xml” put it in resources folder
- Include connection details for the database
- Information to include: database URL, username, password, SQL dialect
Here is an example of a 1- Basic hibernate.cfg.xml configuration file:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="connection.username">myusername</property>
<property name="connection.password">mypassword</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Mapping files -->
<mapping resource="com/mypackage/mypersistentobject.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Detailed explanation –
XML Part / Feature | Meaning | Real-World Example | Impact on XML File |
---|---|---|---|
XML Version | 1.0 (Standard) / 1.1 (Rare) | Version of a software update | 1.0 is universal; 1.1 supports rare characters. |
Encoding | UTF-8, UTF-16, ISO-8859-1, ASCII | Choosing a keyboard language | UTF-8 supports all languages; ASCII only basic English. |
DOCTYPE & DTD | Defines XML rules | Exam guidelines ensuring correct answers | Ensures correct structure; missing DTD means no validation. |
DTD URL | http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd | A reference manual for exam rules | Hibernate checks this URL to verify XML correctness. |
hibernate.connection.driver_class | Which database driver to use | Choosing a taxi service (Ola, Uber) | Determines which database (MySQL, PostgreSQL, etc.) is used. |
hibernate.connection.url | Where the database is | Google Maps location link | Specifies the database address and name. |
hibernate.connection.username | Database username | Your login username | Identifies the user in the database. |
hibernate.connection.password | Database password | Your login password | Grants access to the database. |
hibernate.dialect | SQL syntax for Hibernate | Choosing a language for communication | Ensures compatibility with different databases. |
hibernate.show_sql | Show SQL in logs | Debug mode in a game | Helps developers see the actual queries being executed. |
hbm2ddl.auto | How to handle tables | Erasing or updating a whiteboard | Controls table creation, updates, and deletion. |
<mapping resource> | Maps Java class to database table | A blueprint for object-to-table conversion | Defines which Java class is linked to which DB table. |
Note – There are several alternative ways to create a Hibernate configuration file, instead of using a hibernate.cfg.xml file:
2- Using a Java class: A Java class can be used to create a Hibernate configuration object, which can be used to configure Hibernate.
import org.hibernate.cfg.Configuration;
public class HibernateConfig {
public static Configuration getConfig() {
Configuration config = new Configuration();
config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydatabase");
config.setProperty("hibernate.connection.username", "myusername");
config.setProperty("hibernate.connection.password", "mypassword");
config.setProperty("hibernate.connection.pool_size", "1");
config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
config.setProperty("hibernate.current_session_context_class", "thread");
config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
config.setProperty("hibernate.show_sql", "true");
config.setProperty("hibernate.hbm2ddl.auto", "create");
config.addResource("com/mypackage/mypersistentobject.hbm.xml");
return config;
}
}
This is the appropriate weblog for anybody who wants to find out about this topic. You realize so much its virtually onerous to argue with you (not that I truly would need匟aHa). You positively put a brand new spin on a topic thats been written about for years. Great stuff, simply nice!
One more important component is that if you are a senior citizen, travel insurance regarding pensioners is something you need to really contemplate. The older you are, the more at risk you will be for having something terrible happen to you while in foreign countries. If you are definitely not covered by quite a few comprehensive insurance plan, you could have a few serious issues. Thanks for expressing your guidelines on this web site.
The crux of your writing while sounding reasonable originally, did not sit properly with me personally after some time. Someplace throughout the paragraphs you were able to make me a believer but just for a very short while. I nevertheless have a problem with your jumps in assumptions and you would do well to fill in those gaps. When you can accomplish that, I will surely end up being impressed.
A person necessarily help to make seriously posts I might state. This is the first time I frequented your website page and thus far? I surprised with the research you made to create this particular publish incredible. Wonderful task!
Good day I am so glad I found your webpage, I really found you by accident, while I was researching on Google for something else, Anyhow I am here now and would just like to say kudos for a remarkable post and a all round entertaining blog (I also love the theme/design), I don抰 have time to look over it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome job.
Very good blog! Do you have any hints for aspiring writers? I’m hoping to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips? Cheers!
Useful info. Lucky me I found your web site accidentally, and I’m stunned why this accident did not took place in advance! I bookmarked it.
I simply wished to thank you so much again. I do not know what I would have gone through without the entire solutions documented by you about such question. It seemed to be a real frightful circumstance for me, but discovering this professional approach you handled the issue forced me to jump with gladness. Now i’m grateful for the information and then wish you find out what a great job that you’re providing training the rest through your web blog. I am certain you have never got to know any of us.
Thanks a lot for the helpful article. It is also my belief that mesothelioma cancer has an very long latency period, which means that the signs of the disease may not emerge until 30 to 50 years after the preliminary exposure to mesothelioma. Pleural mesothelioma, which can be the most common style and has an effect on the area across the lungs, may cause shortness of breath, torso pains, and a persistent coughing, which may cause coughing up blood.
Good day! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!
It’s a pity you don’t have a donate button! I’d most certainly donate to this outstanding blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my Facebook group. Talk soon!
Hello, i feel that i saw you visited my website thus i return the desire?I am trying to to find things to improve my site!I assume its ok to make use of some of your concepts!!
Good day! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Appreciate it!
Hi just wanted to give you a brief heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.
There are some interesting cut-off dates on this article but I don抰 know if I see all of them heart to heart. There is some validity but I’ll take hold opinion till I look into it further. Good article , thanks and we wish more! Added to FeedBurner as properly
Howdy this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be greatly appreciated!
Magnificent website. Lots of useful info here. I am sending it to some friends ans also sharing in delicious. And certainly, thanks for your sweat!
I am really inspired along with your writing skills as neatly as with the format for your blog. Is this a paid theme or did you modify it your self? Anyway stay up the nice quality writing, it is uncommon to look a nice weblog like this one today!
[…] Step 2 – Configuring Hibernate […]