Developing sample application using JPA (Hibernate)

Pervious relative links:

Creation of simple web application Maven by using Eclipse

To know How to configuration and setup JPA (Hibernate).

Create a UI (User Interface) that is to build the UI using the JSP or HTML.

Why using UI:

In general UI is used to communicate between the server and end-user (Client). In my case UI is developed by JSP. Like shown below.

Log into Geek

When the user fills fields that is USERNAME,PASSWORD and when he clicks on the LOGIN button, the control will be go to the AdminServlet. In this Servlet, read the values from request.

Prg for Authentication

Here the Validate class can have the method is‘isAuthenticater’ in this method checks the given username and password is exist.

Using these values, we need to validate the UseName and Password. In Validation class the given username and password values are authenticate. In this below code we are using the ‘CriteriaBuilder’. The CriteriaBuilder use to build the query and executes the query.


In the below program code predicating the data values:

package com.gk.validation; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import com.gk.model.EntityManagerProvider; import com.gk.pojoclass.Admin; public class Validate { //className Validate public static boolean isAuthenticater (String user, String pwd) { isAuthenticater method is used for validate the user and password //creation of builder EntityManager entityManager = EntityManagerProvider .INSTANCE.getEntityManager(); CriteriaBuilder builder = entityManager. getCriteriaBuilder(); CriteriaQuery cq = builder.createQuery(Admin.class); //select *from Root r = cq.from(Admin.class);; //where Predicate usereq = builder.equal(r.get("username"),user); Predicate pwdeq = builder.equal(r.get("password"), pwd); Predicate predicate = builder.and(usereq,pwdeq); cq.where(predicate); //execution Admin admin = entityManager.createQuery(cq).getSingleResult(); return admin != null; } }

How to use JPA Predicate:

The way of predicting to a data shown below


The highlighted code with flower braces is the place where the query generated.

How to insert given data values from client in JPA:

In below showing the JSP in this JSP inserting the values from client. Now inserted values are go to store into DB.


In the .html/.jsp file it is mandatory it mention action attribute in shown below.

mentioning_the _action

Here is the action for what code or for what program do this action to be performed.

package com.gk.Servlets; import; import javax.persistence.EntityManager; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gk.model.EntityManagerProvider; import com.gk.pojoclass.CourseReg; public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CourseServlet() { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cname = request.getParameter("cname"); String cfee = request.getParameter("cfee"); String cduration = request.getParameter("cduration"); EntityManager entityManager = EntityManagerProvider .INSTANCE.getEntityManager(); entityManager.getTransaction().begin(); CourseReg cr = new CourseReg();//here courseReg is a pojo class cr.setCname(cname); cr.setCfee(cfee); cr.setCduration(cduration); entityManager.persist(cr);//here persisting the data. entityManager.getTransaction().commit(); request.setAttribute("course", cr); RequestDispatcher s = request. getRequestDispatcher("/jsp/student.jsp"); s.forward(request, response); } }

In the above code, CourseServlet is the program to be performed with thisaction.doPost is the method which is given in the servlet. In this EntityManager INSTANCE use to get the EntityManager. After that based on the EntityManager instance start the transation it’s shown below:


In above code using the EntityManager instance calls getTransaction().begin();

Now transaction will be started. In my case the above code takingCourseReg POJO class. Create the instance of CourseReg pojo class. Using that instance of CourseReg set the values. After that persiste the data using the EntityManager instance.


Here forwarding to Student.jsp page using RequestDispatcher from servlet class.

 Request Dispatcher
See the below output screen :
For source code.
HTML Comment Box is loading comments...