Jpa MVC
Jpa MVC
JPA (Java Persistence API) is also called as “Jakarta Persistence API”, The Java Persistence
API (JPA) is a specification of Java. It is used to persist data between Java object and relational
database. JPA acts as a bridge between object-oriented domain models and relational database
systems.
As JPA is just a specification, we don‟t write any SQL Query here, ORM (Object Relational
Mapping) tools like Hibernate, TopLink and iBatis does itself.
Example: Let‟s create a table in database with name “Student”.
Create a class with same name as table in DB, & create variable equal to the no. of columns in
that table & make getters & setters for that variables. This class is called as “Entity Class”.
Now create the object of the entity class and put all the data in that object like id, name, course
and fee, now JPA takes the data of the object and puts it into the table in database & the ORM
which implements this in is called Hibernate (We‟re using Hibernate).
Q . What is Hibernate?
A. Hibernate is an ORM (Object Relational Mapping) tool. JPA defines the concept of ORM, but
implementation of that is done in Hibernate, Eclipse Link etc..
As a beginner when working on Spring Boot Projects, follow the below steps:
1. Create Database Schema (Tables)
2. Create Entity Class
3. Configure “application.properties” file (with DB details)
4. Create Repository Layer (Interface)
5. Perform JUnit Testing
The first 3 steps belongs to Hibernate (anything imported from “javax.persistence” package
belongs to Hibernate), 4th Step belong to Spring Boot (Repository layer has lot of built-in
methods using which we can perform CRUD Operations with DB).
1
ADVANCE JAVA
Example: Let‟s create a project named “demo_crud” to perform CRUD Operations on Student‟s
data.
Step #1: Create Database Schema (Tables)
create database demo_crud
use demo_crud
create table student
(
ID int primary key auto_increment,
Name varchar (45),
Course varchar (45),
Fee int
)
@Entity //This annotation describes that this is a Special class whose object data will
be stored in DB table, Entity=Table in DB
public class Student {
//variable name should match with the column names in DB
@Id //It declares that this is a Primary Key
@GeneratedValue(strategy = GenerationType.IDENTITY) //It declares that the Id has
auto increment in its values
private long id; //Using Long" instead of "int" here because, Long can store more
data than int
private String name;
private String course;
private int fee;
2
ADVANCE JAVA
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void saveOneStudent() {
Student s1 = new Student();
s1.setName("Mike");
s1.setCourse("Development");
s1.setFee(1000);
studentRepo.save(s1);
}
}
3
ADVANCE JAVA
Now we have successfully saved the data of one student in MySQL DB without writing Query.
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void saveOneStudent() {
Student s1 = new Student();
s1.setName("Stallin");
s1.setCourse("Testing");
s1.setFee(800);
studentRepo.save(s1);
}
}
We can also delete the data of any student using ID#, but the deleted ID# will not be assigned
to the next record, once deleted, permanently deleted, because we‟re using primary key for
IDs, so every entry has unique ID#, so the next save method will insert ID#3 after deleting
Stallin‟s record which is assigned ID#2.
4
ADVANCE JAVA
package com.demo_crud;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.demo_crud.entities.Student;
import com.demo_crud.repository.StudentRepository;
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void saveOneStudent() {
Student s1 = new Student();
s1.setName("Stallin");
s1.setCourse("Testing");
s1.setFee(800);
studentRepo.save(s1);
}
@Test
void deleteOneStudent() {
studentRepo.deleteById(2L); //Deleting ID#2 (Stallin's Record)
//Just select delete method name and run, if we run directly the above save
method will also run and again same data will be saved with next ID number
}
}
5
ADVANCE JAVA
Before going forward in Student Project‟s CRUD Operations, let‟s learn about Optional Class.
OPTIONAL CLASS
Java 8 introduced a new public final class “Optional” in java.util package. It is used to
deal with NullPointerException in java application. It provides the methods to easily check
whether a variable has null value or not, this is an alternative way of handling
NullPointerException other than Try Catch Block, the commonly used methods of Java Optional
class are:
Optional.ofNullable(): It returns a Non-empty Optional if the given object has a value,
otherwiseit returns an empty Optional.
isPresent(): It is used check whether the particular Optional object is empty or no-empty.
ifPresent(): It only executes if the given Optional object is non-empty.
Example: (with NullPointerException)
package p1;
public class A {
int x =10;
static A a1; //a1 is null reference variable
public static void main(String[] args) {
System.out.println(a1.x);
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at p1.A.main(A.java:6)
Output:
Optional.empty
false
6
ADVANCE JAVA
(Applying if condition, If value is present, it prints value, otherwise else part)
package p1;
import java.util.Optional;
public class A {
int x =10;
static A a1 = new A(); //a1 is not null
public static void main(String[] args) {
Optional<A> val = Optional.ofNullable(a1);
if(val.isPresent()) {
System.out.println(a1.x);
}else {
System.out.println("No Value Present");
}
}
}
Output:
10
Output:
No Value Present
7
ADVANCE JAVA
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void saveOneStudent() {
Student s1 = new Student();
s1.setName("Sameer");
s1.setCourse("Development");
s1.setFee(5000);
studentRepo.save(s1);
}
@Test
void deleteOneStudent() {
studentRepo.deleteById(2L); //Deleting ID#2 (Stallin's Record)
}
@Test
void getOneStudent() {
Optional<Student> findById = studentRepo.findById(1L); //ID#1 is present
if(findById.isPresent()){ //it will put the return data in optional object
Student student = findById.get();
//"get" will convert it to student(entity) object
System.out.println(student.getId());
System.out.println(student.getName());
System.out.println(student.getCourse());
System.out.println(student.getFee());
}else {
System.out.println("No Record Found");
}
}
}
Output:
1
Mike
Development
1000
8
ADVANCE JAVA
Let‟s find by ID#10 (ID#10 is not present in database
package com.demo_crud;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.demo_crud.entities.Student;
import com.demo_crud.repository.StudentRepository;
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void saveOneStudent() {
Student s1 = new Student();
s1.setName("Sameer");
s1.setCourse("Development");
s1.setFee(5000);
studentRepo.save(s1);
}
@Test
void deleteOneStudent() {
studentRepo.deleteById(2L); //Deleting ID#2 (Stallin's Record)
}
@Test
void getOneStudent() {
Optional<Student> findById = studentRepo.findById(10L); //ID#10 isn’t present
if(findById.isPresent()){ //it will put the return data in optional object
Student student = findById.get();
//"get" will convert it to student(entity) object
System.out.println(student.getId());
System.out.println(student.getName());
System.out.println(student.getCourse());
System.out.println(student.getFee());
}else {
System.out.println("No Record Found");
}
}
}
Output:
No Record Found
If we wouldn‟t have used Optional Class here, as there was no data with ID#10 was present in
DB, it would have resulted “NullPointerException”, because findById is null, & with null ref
variable if we call get method it is “NullPointerException”.
9
ADVANCE JAVA
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void updateOneStudent() { //Updating ID#1
Optional<Student> findById = studentRepo.findById(1L);
if(findById.isPresent()){
Student student = findById.get();
student.setCourse("Testing");
student.setFee(800);
studentRepo.save(student);
}else {
System.out.println("No Record Found");
}
}
}
10
ADVANCE JAVA
package com.demo_crud;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.demo_crud.entities.Student;
import com.demo_crud.repository.StudentRepository;
@SpringBootTest
class DemoCrudApplicationTests {
@Test
void getAllStudent() {
Iterable<Student> findAll = studentRepo.findAll();
for (Student s : findAll) {
System.out.println(s.getId()+" "+s.getName()+" "+s.getCourse()+" "+s.getFee());
}
}
}
Output:
1
3
4
Answer:
@Test
void getAllStudent() {
Iterable<Student> findAll = studentRepo.findAll();
findAll.forEach( (s) -> { //Using Lambda Expression
System.out.println(s.getId()+" "+s.getName()+" "+s.getCourse()+" "+s.getFee()); });
}
11
ADVANCE JAVA
Here we are going to develop a project for “Marketing Lead”, (“Lead” is a marketing
terminology, A potential customer enquiring about the product is called as lead).
For this project on Spring Boot, we are following the below steps:
1. Create Database Schema (Tables) using JAVA Code
2. Create Entity Class
3. Configure “application.properties” file (with DB details)
4. Create Repository Layer (Interface)
5. Configure Jasper Tomcat
6. View Layer
7. Controller Layer
8. Service Layer
Step #1: Create Database Schema (Local instance MySQL>Create new Schema)
@Entity
@Table(name = "leads") //Annotation should be used when table name needed in DB is dif-
ferent than class name
public class Lead {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //primary & auto increment
private long id;
12
ADVANCE JAVA
@Column(name = "first_name", nullable=false)
//Annotation should be used when column name needed in DB is different than method
name, it shouldn't have null value
private String firstName;
13
ADVANCE JAVA
Step #3: Configure “application.properties” file (with DB details)
spring.datasource.url=jdbc:mysql://localhost:3306/marketing_lead_db
spring.datasource.username=root
spring.datasource.password=Test
package com.marketing.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.marketing.entities.Lead;
public interface LeadRepository extends JpaRepository<Lead, Long> { //Lead is Entity Class
}
14
ADVANCE JAVA
</form>
${msg}
</body>
</html>
15
ADVANCE JAVA
After creating “create_lead.jsp” and “Menu.jsp” let‟s go ahead with Controller Layers.
Step #6: Controller Layers
@Controller
public class LeadController {
//handler method, To navigate to “create_lead.jsp”, because only controller can access this
@RequestMapping("/createLead") //acts like @WebServlet
public String viewCreateLeadPage() {
return "create_lead"; //acts like RequestDispatcher
//path of this jsp file given in "application.properties"
}
A.
16
ADVANCE JAVA
@Service
public class LeadServiceImpl implements LeadService {
@Override
public void saveLead(Lead l) {
leadRepo.save(l); //To save data in to the database
}
@Override
public List<Lead> listLeads() {
List<Lead> leads = leadRepo.findAll(); //To read data in the database
return leads;
}
@Override
public void deleteLeadById(long id) {
leadRepo.deleteById(id); //To delete data from the database
}
@Override
public Lead getOneLead(long id) {
Optional<Lead> findById = leadRepo.findById(id); //findrecord by ID# in the database
Lead lead = findById.get();
return lead;
}
}
Note: Every time restarting the servers manually is painful, let‟s make it automatic so that
whenever we make any changes to our project, server should automatically restart (Live
Loading). To do that Right Click on the project, & in cascade menu select Spring>Add DevTools.
Method #2: To Save data from View Layer to the database (LeadController.java)
//Method#2: it will make the method argument lengthy if working with huge no. of fields, Recom-
mended for less no. of fields
@RequestMapping("/saveLead")
public String saveOneLead(@RequestParam("name") String fName, @RequestParam("LastName")
String lName, @RequestParam("emailId") String mail, @RequestParam("mobileNumber") long mobile) {
Lead l = new Lead(); //lead Object is needed to save data in DB
l.setFirstName(fName);
l.setLastName(lName);
l.setEmail(mail);
l.setMobile(mobile);
leadService.saveLead(l);
return "create_lead";
}
Method #3: To Save data from View Layer to the database (LeadController.java)
//Method#3: DTO- Data Transfer Object
@RequestMapping("/saveLead")
public String saveOneLead(LeadData data, ModelMap model) {
//Press Ctr+1, Create "LeadData" class in dto package
Lead ld = new Lead(); //lead Object is needed to save data in DB
ld.setFirstName(data.getFirstName());
ld.setLastName(data.getLastName());
ld.setEmail(data.getEmail());
ld.setMobile(data.getMobile());
leadService.saveLead(ld);
model.addAttribute("msg", "Lead Saved Successfully");
return "create_lead";
}
Database:
Core tags The JSTL core tag provide variable support, URL management, flow control,
etc. The URL for the core tag is http://java.sun.com/jsp/jstl/core. The prefix of
core tag is c.
Function The functions tags provide support for string manipulation and string length.
tags The URL for the functions tags is http://java.sun.com/jsp/jstl/functions and
prefix is fn.
Formatting The Formatting tags provide support for message formatting, number and
tags date formatting, etc. The URL for the Formatting tags
is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.
XML tags The XML tags provide flow control, transformation, etc. The URL for the XML
tags is http://java.sun.com/jsp/jstl/xml and prefix is x.
SQL tags The JSTL SQL tags provide SQL support. The URL for the SQL tags
is http://java.sun.com/jsp/jstl/sql and prefix is sql.
Source
The JSTL.jar file is needed run the JSTL Tags, Click below to download JSTL_1.2.jar
Example #1:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> <!-- JSTL Directive Tag -->
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title> JSTL Tag </title>
</head>
<body>
<c:set var="val" value="100"></c:set>
<c:out value="${val}"></c:out>
</body>
</html>
Example #3:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> <!-- JSTL Directive Tag -->
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title> JSTL Tag </title>
</head>
<body>
<c:forEach var="j" begin="1" end="5">
<p>${j}</p>
</c:forEach>
</body>
</html>
Add the dependency tag to download & use JSTL 1.2 in Spring Boot.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version><!--$NO-MVN-MAN-VER$-->
</dependency>
After adding the dependency jar, don‟t rely on auto start, re-start server manually to configure
JSTL jar completely.
“listAll” method from “LeadController.java” will return the “leadSearchResult.jsp”.
List of all the Leads JSP Page (leadSearchResult.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="Menu.jsp" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> <!-- JSTL Directive Tag -->
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>All Leads</title>
</head>
<body>
<table border=1>
<tr><th colspan="7" bgcolor="lightgrey"><font size="6"> LIST OF LEADS </font></th></tr>
<tr bgcolor="lightgrey">
<th>Id No.</th><th>First Name</th><th>Last Name</th><th>Email</th><th>Mobile</th><th col-
span="2">Action</th>
</tr>
<c:forEach var="leads" items="${lds}">
<tr><td style="text-align:center"> ${leads.id} </td>
<td> ${leads.firstName} </td>
<td>${leads.lastName}</td>
<td>${leads.email}</td>
<td>${leads.mobile}</td>
<td><a href="delete?id=${leads.id}">
<input style="background-color:mistyrose; color:darkred" type="submit" value="Delete">
</a></td>
<td><a href="update?id=${leads.id}">
<input style="background-color:lightcyan; color:darblue" type="submit" value="Update">
</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Localhost:8080/listAll
This will redirect back to the “leadSearchResult.jsp” with the updated details.
XML Consumer/Client
J2EE
XML Exposer
J2EE
Example:
Create a class named “LeadRestController.java” in Controller layer of project “marketing”
package com.marketing.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.marketing.entities.Lead;
import com.marketing.services.LeadService;
@GetMapping
public List <Lead> getAllleads() {
List<Lead> leads = leadService.listLeads();
return leads;
}
}
Web Output:
Web Services is nothing but developing a url to interact with the database.
There are two ways we can implement web services:
1. SOAP: Here we exchange the data between the applications using XML file,
implementation of SOAP web services is complex, because we need to parse
programmatically XML file.
2. REST: In Rest web services we exchange the data between applications using JSON
Object (JAVA Script Object Notation), Implementation of Rest services is easy. However
Rest services also supports XML files.
JSON stands for javascript object notation. XML stands for an extensible markup language.
The extension of json file is .json. The extension of xml file is .xml.
The internet media type is application/json. The internet media type is application/xml or text/xml.
The type of format in JSON is data interchange. The type of format in XML is a markup language.
The object created in JSON has some type. XML data does not have any type.
The data types supported by JSON are strings, XML data is in a string format.
numbers, Booleans, null, array.
It does not have any capacity to display the data. XML is a markup language, so it has the capacity to display the
content.
JSON has no tags. XML data is represented in tags, i.e., start tag and end tag.
JSON is quicker to read and write. XML file takes time to read and write because the learning curve is
higher.
JSON can use arrays to represent the data. XML does not contain the concept of arrays.
It can be parsed by a standard javascript function. XML data which is used to interchange the data, must be parsed with
It has to be parsed before use. respective to their programming language to use that.
JSON Example:
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
XML Example:
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
leadService.saveLead(lead);
Enter the url of the api using “POST” method in Postman. Go to Body>Raw>JASON
and input the data as per the JASON object.
Here we‟re going to save Sobia‟s data into the database.
Data currently present in the database:
List of all the leads after saving Sobia‟s data using Postman:
List of the all the leads after updating John‟s (ID#4) data:
All the CRUD Operations done using Postman also called as “Exposing of the data using Web
Services”.
Now Let‟s “Consume the data using Web Services” & build one project to search the lead data
using ID# & that project will run on another server port.
To build this part of the project we need the above built api (url):
“localhost:8080/api/leads/leadinfo/5”
Let‟s check this url in the browser:
Here we get the JSON object, based on that we‟ll start our project by creating the dto package
and a JAVA class matching to these variables because we have to copy the data from JSON
object to JAVA object.
Value (Long) Value (String) Value (String) Value (String) Value (Long)
Created the “com.search_lead.dto” package and a “Lead.java” JAVA class matching to the JSON
Object because we have to copy the data from JSON object to JAVA object.
@Service
public class SearchLeadServiceImpl implements SearchLeadService {
@Override
public Lead getLeadById(long id) {
RestTemplate rt = new RestTemplate(); //Built-in Class for consuming Web Services
Lead lead = rt.getForObject("http://localhost:8080/api/leads/leadinfo/"+id, Lead.class);
//take the json object & save in Lead Class
return lead;
}
}
#Server Port
server.port=9090
POM.xml (Dependency)
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>