0% found this document useful (0 votes)
17 views5 pages

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)
17 views5 pages

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

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

>

<!DOCTYPE hibernate-configuration PUBLIC

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

"[Link]

<hibernate-configuration>

<session-factory>

<property name="[Link].driver_class">[Link]</property>

<property name="[Link]">jdbc:mysql://localhost:3306/your_database</
property>

<property name="[Link]">your_username</property>

<property name="[Link]">your_password</property>

<property name="[Link]">[Link]</property>

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

<property name="[Link]">update</property>

<!-- Mapping files -->

<mapping class="[Link]"/>

</session-factory>

</hibernate-configuration>

import [Link].*;

@Entity

@Table(name = "students")

public class Student {

@Id

@GeneratedValue(strategy = [Link])

private Long id;


@Column(name = "name")

private String name;

@Column(name = "age")

private int age;

// Constructors, getters, and setters

import [Link];

import [Link];

import [Link];

import [Link];

public class StudentManager {

private SessionFactory sessionFactory;

public StudentManager() {

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

public void addStudent(Student student) {

Transaction transaction = null;

try (Session session = [Link]()) {

transaction = [Link]();

[Link](student);
[Link]();

} catch (Exception e) {

if (transaction != null) {

[Link]();

[Link]();

public Student getStudentById(Long id) {

try (Session session = [Link]()) {

return [Link]([Link], id);

public void updateStudent(Student student) {

Transaction transaction = null;

try (Session session = [Link]()) {

transaction = [Link]();

[Link](student);

[Link]();

} catch (Exception e) {

if (transaction != null) {

[Link]();

[Link]();

}
public void deleteStudent(Student student) {

Transaction transaction = null;

try (Session session = [Link]()) {

transaction = [Link]();

[Link](student);

[Link]();

} catch (Exception e) {

if (transaction != null) {

[Link]();

[Link]();

public void close() {

[Link]();

public class Main {

public static void main(String[] args) {

StudentManager studentManager = new StudentManager();

// Create and add a student


Student student = new Student();

[Link]("John");

[Link](20);

[Link](student);

// Retrieve a student by id

Student retrievedStudent = [Link](1L);

[Link]("Retrieved Student: " + retrievedStudent);

// Update the retrieved student

[Link](21);

[Link](retrievedStudent);

// Delete the student

[Link](retrievedStudent);

[Link]();

You might also like