Hibernate_lab
Hibernate_lab
<dependencies>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.0.Final</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- Jakarta Persistence API -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- JSP and Servlets -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
Create a database named demo in MySQL and a books table using the following
SQL script:
package com.example.entity;
import jakarta.persistence.*;
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "author")
private String author;
@Column(name = "price")
private double price;
package com.example.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import com.example.entity.Book;
import java.util.Properties;
configuration.setProperties(settings);
configuration.addAnnotatedClass(Book.class);
sessionFactory =
configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
package com.example.dao;
import com.example.entity.Book;
import com.example.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
AddBookServlet
package com.example.servlet;
import com.example.dao.BookDao;
import com.example.entity.Book;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/addBook")
public class AddBookServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String title = req.getParameter("title");
String author = req.getParameter("author");
double price = Double.parseDouble(req.getParameter("price"));
resp.sendRedirect("listBooks");
}
}
ListBooksServlet
package com.example.servlet;
import com.example.dao.BookDao;
import com.example.entity.Book
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/listBooks")
public class ListBooksServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List<Book> books = bookDao.getBooks();
req.setAttribute("books", books);
req.getRequestDispatcher("list-books.jsp").forward(req, resp);
}
}
UpdateBookServlet
package com.example.servlet;
import com.example.dao.BookDao;
import com.example.entity.Book;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/updateBook")
public class UpdateBookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Long id = Long.parseLong(req.getParameter("id"));
Book book = bookDao.getBook(id);
req.setAttribute("book", book);
req.getRequestDispatcher("update-book.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Long id = Long.parseLong(req.getParameter("id"));
String title = req.getParameter("title");
String author = req.getParameter("author");
double price = Double.parseDouble(req.getParameter("price"));
Book book = new Book(id, title, author, price);
bookDao.updateBook(book);
resp.sendRedirect("listBooks");
}
}
DeleteBookServlet
package com.example.servlet;
import com.example.dao.BookDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/deleteBook")
public class DeleteBookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Long id = Long.parseLong(req.getParameter("id"));
bookDao.deleteBook(id);
resp.sendRedirect("listBooks");
}
}
add-book.jsp
<!DOCTYPE html>
<html>
<head>
<title>Add Book</title>
</head>
<body>
<h2>Add Book</h2>
<form action="addBook" method="post">
Title: <input type="text" name="title" required><br>
Author: <input type="text" name="author" required><br>
Price: <input type="text" name="price" required><br>
<input type="submit" value="Add Book">
</form>
<a href="listBooks">View Books</a>
</body>
</html>
list-books.jsp
<!DOCTYPE html>
<html>
<head>
<title>List Books</title>
</head>
<body>
<h2>List of Books</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Actions</th>
</tr>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.id}</td>
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<a href="updateBook?id=${book.id}">Edit</a>
<a href="deleteBook?id=${book.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<a href="addBook">Add New Book</a>
</body>
</html>
update-book.jsp
<!DOCTYPE html>
<html>
<head>
<title>Update Book</title>
</head>
<body>
<h2>Update Book</h2>
<form action="updateBook" method="post">
<input type="hidden" name="id" value="${book.id}">
Title: <input type="text" name="title" value="${book.title}"
required><br>
Author: <input type="text" name="author" value="${book.author}"
required><br>
Price: <input type="text" name="price" value="${book.price}"
required><br>
<input type="submit" value="Update Book">
</form>
<a href="listBooks">View Books</a>
</body>
</html>