JV Updated
JV Updated
Pom.xml
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
Student.java
package com.example.hibernate;
import javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity public
class Student {
@Id
@GeneratedValue(strategy =
GenerationType.IDENTITY) private int id; private
String name; private String email; private int age; //
Default constructor public Student() {} // Constructor
with parameters
return name;
this.name = name;
return email;
this.email = email;
this.age = age;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", email=" + email + ", age=" + age +
"]";
hibernate.cfg.xml
3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property
name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</propert
y> <!--
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Echo all executed queries to stdout -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property
name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvid
er</pro perty>
</session-factory>
</hibernate-configuration>
StudentDAO.java
package com.example.hibernate;
import org.hibernate.Session;
import
org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class).buil
dSessi onFactory();
transaction.commit();
transaction.commit();
session.delete(student);
transaction.commit();
MainApp.java
package com.example.hibernate;
student.setAge(26);
studentDAO.updateStudent(student);
studentDAO.deleteStudent(student.getId());
studentDAO.close();
Output:-
25. Create a Java application using Hibernate to perform a CRUD operation using
Hibernate Query Language (HQL). Instructions: 1. Define a Product entity with fields id,
name, price, and quantity. 2. Implement methods to: o Insert new Product objects into
the database. o Retrieve all Product objects using HQL. o Update a Product object. o
Delete a Product object by ID. 3. Test the CRUD operations by creating instances of
Product and invoking these methods.
Pom.xml
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.6.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
Product.java package
com.example.model; import
javax.persistence.Entity;
import javax.persistence.Id;
import
javax.persistence.Table;
@Entity
@Table(name = "product")
public class Product
this.id = id;
return name;
this.name = name;
return price;
this.price = price;
this.quantity = quantity;
@Override
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mention annotated class -->
<mapping class="com.example.model.Product"/>
</session-factory>
</hibernate-configuration>
ProductDAO.java package
com.example.dao; import
com.example.model.Product;
import org.hibernate.Session;
import
org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import
org.hibernate.query.Query;
this.sessionFactory = sessionFactory;
session.close();
return products;
}
public void updateProduct(Product product) {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.update(product); transaction.commit();
session.close();
Transaction transaction =
session.beginTransaction(); Product product =
session.get(Product.class, productId); if (product !=
null) { session.delete(product);
}
transaction.commit(
);
session.close();
HibernateUtil.java package
com.example.util; import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
public class HibernateUtil {
sessionFactory = new
Configuration().configure("hibernate.cfg.xml").buildSessionFacto
ry(); } catch (Exception e) { e.printStackTrace();
getSessionFactory().close();
import java.util.List;
public class Main {
System.out.println(product);
}
// Update a product
productDAO.updateProduct(productToUpdate);
// Delete a product by ID
productDAO.deleteProduct(1); // Retrieve all
products after deletion products =
productDAO.getAllProducts();
System.out.println("All Products After Deletion: ");
for (Product product : products) {
System.out.println(product);
// Shutdown Hibernate
HibernateUtil.shutdown();
Output:-