0% found this document useful (0 votes)
8 views

XML Version

The document shows how to configure Hibernate to connect to a MySQL database and map Java classes to database tables. It defines a Student entity with ID, name, and age properties and uses Hibernate to add, retrieve, update, and delete Student records from the database.

Uploaded by

Tttt Ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

XML Version

The document shows how to configure Hibernate to connect to a MySQL database and map Java classes to database tables. It defines a Student entity with ID, name, and age properties and uses Hibernate to add, retrieve, update, and delete Student records from the database.

Uploaded by

Tttt Ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

<?xml version="1.0" encoding="utf-8"?

>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">com.mysql.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>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping files -->

<mapping class="com.example.Student"/>

</session-factory>

</hibernate-configuration>

import javax.persistence.*;

@Entity

@Table(name = "students")

public class Student {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;


@Column(name = "name")

private String name;

@Column(name = "age")

private int age;

// Constructors, getters, and setters

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class StudentManager {

private SessionFactory sessionFactory;

public StudentManager() {

sessionFactory = new Configuration().configure().buildSessionFactory();

public void addStudent(Student student) {

Transaction transaction = null;

try (Session session = sessionFactory.openSession()) {

transaction = session.beginTransaction();

session.save(student);
transaction.commit();

} catch (Exception e) {

if (transaction != null) {

transaction.rollback();

e.printStackTrace();

public Student getStudentById(Long id) {

try (Session session = sessionFactory.openSession()) {

return session.get(Student.class, id);

public void updateStudent(Student student) {

Transaction transaction = null;

try (Session session = sessionFactory.openSession()) {

transaction = session.beginTransaction();

session.update(student);

transaction.commit();

} catch (Exception e) {

if (transaction != null) {

transaction.rollback();

e.printStackTrace();

}
public void deleteStudent(Student student) {

Transaction transaction = null;

try (Session session = sessionFactory.openSession()) {

transaction = session.beginTransaction();

session.delete(student);

transaction.commit();

} catch (Exception e) {

if (transaction != null) {

transaction.rollback();

e.printStackTrace();

public void close() {

sessionFactory.close();

public class Main {

public static void main(String[] args) {

StudentManager studentManager = new StudentManager();

// Create and add a student


Student student = new Student();

student.setName("John");

student.setAge(20);

studentManager.addStudent(student);

// Retrieve a student by id

Student retrievedStudent = studentManager.getStudentById(1L);

System.out.println("Retrieved Student: " + retrievedStudent);

// Update the retrieved student

retrievedStudent.setAge(21);

studentManager.updateStudent(retrievedStudent);

// Delete the student

studentManager.deleteStudent(retrievedStudent);

studentManager.close();

You might also like