0% found this document useful (0 votes)
30 views9 pages

Lab 23

The document describes how to configure Hibernate to map Java objects to database tables using annotations and XML configuration files. It shows code for User and Task entity classes mapped with JPA annotations, a Hibernate configuration file, and code to save objects to the database and retrieve them. The output prints the user name and associated task descriptions.

Uploaded by

Utkarsh Singh
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)
30 views9 pages

Lab 23

The document describes how to configure Hibernate to map Java objects to database tables using annotations and XML configuration files. It shows code for User and Task entity classes mapped with JPA annotations, a Hibernate configuration file, and code to save objects to the database and retrieve them. The output prints the user name and associated task descriptions.

Uploaded by

Utkarsh Singh
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/ 9

Coding:-

Hibernate.cfg.xml file:-
<?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>

<!-- Database connection settings for MySQL -->

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>

<property
name="connection.url">jdbc:mysql://localhost:3306/myhiber</property>

<property name="connection.username">root</property>

<property name="connection.password">2002utk</property>

<!-- Hibernate dialect for MySQL -->

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

<!-- Other settings -->

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

<property name="format_sql">true</property>

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

<property name="current_session_context_class">thread</property>

<mapping class="com.tut.User"/>

<mapping class="com.tut.Task"/>
</session-factory>

</hibernate-configuration>

App.java file:-
package com.tut;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class App {

public static void main(String[] args) {

System.out.println("project Started");

Configuration cfg = new Configuration();

cfg.configure("com/tut/hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();

// Create User

User user = new User();

user.setUsername("John Doe");

// Create Tasks

Task task1 = new Task();

task1.setDescription("Task 1");

task1.setUser(user);
Task task2 = new Task();

task2.setDescription("Task 2");

task2.setUser(user);

user.getTasks().add(task1);

user.getTasks().add(task2);

Session session = factory.openSession();

Transaction tx = session.beginTransaction();

// Save User and Tasks

session.save(user);

tx.commit();

// Close the session and session factory

session.close();

factory.close();

Task.java
package com.tut;

import javax.persistence.*;

@Entity
public class Task {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String description;

@ManyToOne

@JoinColumn(name = "user_id")

private User user;

// Constructors, getters, and setters

// (Omitted for brevity)

public Long getId() {

return id;

public void setId(Long id) {

this.id = id;

public String getDescription() {

return description;

public void setDescription(String description) {

this.description = description;
}

public User getUser() {

return user;

public void setUser(User user) {

this.user = user;

User.java
package com.tut;

import javax.persistence.*;

import java.util.ArrayList;

import java.util.List;

@Entity

public class User {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String username;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)

private List<Task> tasks = new ArrayList<>();


// Constructors, getters, and setters

// (Omitted for brevity)

public Long getId() {

return id;

public void setId(Long id) {

this.id = id;

public String getUsername() {

return username;

public void setUsername(String username) {

this.username = username;

public List<Task> getTasks() {

return tasks;

public void setTasks(List<Task> tasks) {

this.tasks = tasks;

}
Main.java:-
package com.tut;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.EntityTransaction;

import javax.persistence.Persistence;

public class Main {

public static void main(String[] args) {

EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("example");

EntityManager entityManager = entityManagerFactory.createEntityManager();

EntityTransaction transaction = entityManager.getTransaction();

// Create User

User user = new User();

user.setUsername("John Doe");

// Create Tasks

Task task1 = new Task();

task1.setDescription("Task 1");

task1.setUser(user);

Task task2 = new Task();

task2.setDescription("Task 2");

task2.setUser(user);
user.getTasks().add(task1);

user.getTasks().add(task2);

// Persist User and Tasks

transaction.begin();

entityManager.persist(user);

transaction.commit();

// Retrieve User and Print Tasks

User retrievedUser = entityManager.find(User.class, user.getId());

System.out.println("User: " + retrievedUser.getUsername());

for (Task task : retrievedUser.getTasks()) {

System.out.println("Task: " + task.getDescription());

entityManager.close();

entityManagerFactory.close();

Output:-

You might also like