HB Setting Up

Before using Hibernate, you need to install it, configure it with a database (MySQL/PostgreSQL), and set up the required configuration files.

Installing Hibernate with Maven:

To install Hibernate in a Maven project, add the following dependencies in your pom.xml file:

Maven Dependencies


pom.xml
<dependencies>
<!-- Hibernate Core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.3.1.Final</version>
</dependency>

<!-- MySQL Connector (for MySQL database) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>

<!-- PostgreSQL Driver (for PostgreSQL database) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.4</version>
</dependency>
</dependencies>


Configuring Hibernate with MySQL/PostgreSQL:

Hibernate requires a configuration file to connect to the database. You can configure it using:

hibernate.cfg.xml (for Hibernate-based configuration)

Hibernate Configuration File: hibernate.cfg.xml

Example for MySQL:
<hibernate-configuration>
<session-factory>
<!-- Database Connection Settings -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>

<!-- Hibernate Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Automatic Table Creation -->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Logging -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
Example for PostgreSQL:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>

Hibernate Properties & Annotations:

Hibernate provides annotations for defining entity mappings, relationships, and constraints.

Student.java
import jakarta.persistence.*;

@Entity // Marks this as a Hibernate Entity (table)
@Table(name = "students") // Specifies table name
public class Student {

@Id // Primary Key
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-increment
private Long id;

@Column(name = "student_name", nullable = false)
private String name;

@Column(unique = true)
private String email;

// Constructors
public Student() {}
public Student(String name, String email) {
this.name = name;
this.email = email;
}

// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
Main.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {
public static void main(String[] args) {
// Create and save Student object
Student student = new Student("John Doe", "john@example.com");
// Create Hibernate session
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class).buildSessionFactory();
Session session = sessionFactory.openSession();
// Start transaction
session.beginTransaction();
session.save(student);
// Commit transaction
session.getTransaction().commit();
// Close session
session.close();
sessionFactory.close();
System.out.println("Hibernate setup successful!");
}
}

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.