Hibernate Tutorial

Core Hibernate

Hibernate Mapping

Hibernate Annotations

Hibernate with Spring Framework

Hibernate with Database

Hibernate Log4j

Inheritance Mapping

Hibernate Example without IDE

Creating a Hibernate project without an IDE involves several manual steps. Here's a concise guide to doing it:

1. Directory Structure

Your project directory might look something like this:

/my-hibernate-app
|-- /src
|   |-- /com
|   |   |-- /example
|   |   |   |-- Book.java
|   |   |   |-- AppMain.java
|   |-- /resources
|   |   |-- hibernate.cfg.xml
|   |   |-- Book.hbm.xml
|-- /lib
|-- compile-and-run.sh (or compile-and-run.bat for Windows)

2. Required Libraries

Download the necessary libraries:

  • Hibernate ORM libraries: https://hibernate.org/orm/downloads/
  • JDBC driver for your database, e.g., MySQL JDBC driver.

Place all the jar files inside the /lib directory.

3. Hibernate Configuration

Place your hibernate.cfg.xml and entity mapping XML (e.g., Book.hbm.xml) inside the /resources directory.

4. Write Your Application

Your entities (e.g., Book.java) and main application (e.g., AppMain.java) will reside inside /src/com/example.

5. Compilation and Execution Script

Create a script (compile-and-run.sh for Linux/Mac or compile-and-run.bat for Windows) to compile and run your application.

For Linux/Mac:

#!/bin/bash

# Compile
javac -classpath .:lib/* src/com/example/*.java -d .

# Run
java -classpath .:lib/*:resources com.example.AppMain

For Windows:

:: Compile
javac -classpath .;lib\* src\com\example\*.java -d .

:: Run
java -classpath .;lib\*;resources com.example.AppMain

To execute your script, open a terminal or command prompt:

chmod +x compile-and-run.sh  # Only required for Linux/Mac to make the script executable
./compile-and-run.sh         # For Linux/Mac

OR

compile-and-run.bat          # For Windows

6. Final Thoughts

This manual approach is tedious compared to using an IDE like Eclipse or IntelliJ IDEA, which provides tools for automatic compilation, code suggestions, and dependency management (with Maven or Gradle). However, understanding the manual process can give you a better grasp of what's happening behind the scenes when an IDE handles things automatically.

  1. Building Hibernate entities without an IDE:

    • Description: Define Hibernate entities without using an IDE by creating Java classes with the necessary annotations and properties manually.
    • Code:
      // Employee.java
      public class Employee {
          private Long id;
          private String firstName;
          private String lastName;
          // Getters and setters
      }
      
  2. Hibernate XML mapping without using IDE:

    • Description: Create Hibernate XML mapping files manually to define the mapping between entities and database tables without using an IDE.
    • Code:
      <!-- Employee.hbm.xml -->
      <hibernate-mapping>
          <class name="com.example.Employee" table="employee">
              <id name="id" type="long">
                  <generator class="native"/>
              </id>
              <property name="firstName" type="string"/>
              <property name="lastName" type="string"/>
          </class>
      </hibernate-mapping>
      
  3. Hibernate configuration without IDE step-by-step:

    • Description: Set up Hibernate configuration step-by-step without using an IDE. This includes creating the hibernate.cfg.xml file and specifying database connection details, dialect, and other settings.
    • Code:
      <!-- hibernate.cfg.xml -->
      <hibernate-configuration>
          <session-factory>
              <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
              <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</property>
              <property name="hibernate.connection.username">your_username</property>
              <property name="hibernate.connection.password">your_password</property>
              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
              <!-- other properties -->
          </session-factory>
      </hibernate-configuration>
      
  4. Hibernate annotations example without IDE:

    • Description: Use Hibernate annotations without an IDE by manually adding annotations to Java entities. Annotations such as @Entity, @Table, and @Column are used.
    • Code:
      // Employee.java
      @Entity
      @Table(name = "employee")
      public class Employee {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;
      
          @Column(name = "first_name")
          private String firstName;
      
          @Column(name = "last_name")
          private String lastName;
      
          // Getters and setters
      }