Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
Creating a Hibernate project without an IDE involves several manual steps. Here's a concise guide to doing it:
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)
Download the necessary libraries:
Place all the jar files inside the /lib
directory.
Place your hibernate.cfg.xml
and entity mapping XML (e.g., Book.hbm.xml
) inside the /resources
directory.
Your entities (e.g., Book.java
) and main application (e.g., AppMain.java
) will reside inside /src/com/example
.
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
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.
Building Hibernate entities without an IDE:
// Employee.java public class Employee { private Long id; private String firstName; private String lastName; // Getters and setters }
Hibernate XML mapping without using IDE:
<!-- 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>
Hibernate configuration without IDE step-by-step:
hibernate.cfg.xml
file and specifying database connection details, dialect, and other settings.<!-- 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>
Hibernate annotations example without IDE:
@Entity
, @Table
, and @Column
are used.// 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 }