Setting Up Hibernate
Set up a minimal Hibernate project: add the Maven dependency, a JDBC driver, and verify everything is wired up correctly.
Introduction
With the architecture concepts in place, it is time to set up an actual project. This lesson walks through creating a minimal Maven-based Hibernate project: the required dependencies, a JDBC driver for MySQL, and the folder structure you will use throughout the rest of this course.
- What you need installed before starting.
- How to add the Hibernate ORM dependency in Maven.
- How to add a JDBC driver for your target database.
- What a minimal Hibernate project structure looks like.
Prerequisites
- Java Development Kit (JDK) 17 or later installed.
- Apache Maven installed and available on your PATH.
- A running relational database — this course uses MySQL in examples, but any JDBC-supported database works.
- A code editor or IDE (IntelliJ IDEA, Eclipse, or VS Code with Java extensions).
Creating a Maven Project
Start from a plain Maven project. You can generate one from the command line, or create it directly in your IDE.
<project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion>
<groupId>com.programinds</groupId> <artifactId>hibernate-demo</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<dependencies> <!-- Hibernate and JDBC driver dependencies go here --> </dependencies></project>Adding the Hibernate Dependency
Add the core Hibernate ORM artifact to your dependencies. This pulls in Hibernate itself along with its required transitive dependencies (including a JPA API implementation).
<dependencies> <dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>6.5.2.Final</version> </dependency></dependencies>Always check Maven Central for the latest stable Hibernate 6.x version before starting a real project — version numbers shown in examples like this age quickly.
Adding a JDBC Driver
Hibernate needs a JDBC driver to actually talk to your specific database. Without it, Hibernate has no way to open a physical connection, even with correct configuration. Add the driver matching your database.
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.4.0</version></dependency>| Database | Maven Artifact |
|---|---|
| MySQL | com.mysql:mysql-connector-j |
| PostgreSQL | org.postgresql:postgresql |
| Oracle | com.oracle.database.jdbc:ojdbc11 |
| SQL Server | com.microsoft.sqlserver:mssql-jdbc |
Adding hibernate-core alone is not enough. If you skip the JDBC driver dependency, Hibernate will fail at runtime with an error like "No suitable driver found," even though your Hibernate configuration looks correct.
Project Structure
A minimal Hibernate project follows the standard Maven layout, with your entity classes and Hibernate configuration file placed as shown below.
hibernate-demo/├── pom.xml└── src/ └── main/ ├── java/ │ └── com/programinds/ │ ├── Student.java │ └── Main.java └── resources/ └── hibernate.cfg.xmlThe next lesson covers exactly what goes inside hibernate.cfg.xml — connection details, dialect, and mapping registration.
Common Mistakes
- Forgetting to add a JDBC driver dependency alongside hibernate-core.
- Mixing Hibernate versions across dependencies, causing hard-to-diagnose classpath conflicts.
- Placing hibernate.cfg.xml outside of src/main/resources, where Hibernate will not find it by default.
- Using an unsupported or outdated JDK version for the target Hibernate release.
Best Practices
- Always check Maven Central for the current stable versions of hibernate-core and your JDBC driver.
- Keep configuration files in src/main/resources so they land on the classpath automatically.
- Start with a single entity to verify the setup works before mapping your full schema.
- Commit a working pom.xml early so teammates can reproduce the same environment.
Frequently Asked Questions
No. Hibernate works perfectly well as a standalone library, as shown in this course. Spring (or Spring Boot) is optional and adds its own convenience layer on top.
Yes, the same dependencies apply — only the syntax for declaring them differs between Maven's pom.xml and Gradle's build.gradle.
Use the latest stable Hibernate 6.x release for new projects — it targets the Jakarta Persistence (JPA) namespace used throughout this course.
Yes, Hibernate needs a real, running database (or an in-memory one like H2) to connect to — it does not simulate a database on its own.
Key Takeaways
- A Hibernate project needs the hibernate-core dependency plus a matching JDBC driver.
- Missing the JDBC driver is one of the most common early setup mistakes.
- Hibernate does not require Spring — it works as a standalone library.
- Configuration files belong in src/main/resources so Hibernate can find them.
Summary
With hibernate-core and a JDBC driver added to your project, the dependencies are in place. Next, you will fill in hibernate.cfg.xml with real connection settings and the SQL dialect Hibernate should generate.