Unit - 3-2
Unit - 3-2
Java JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute
the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API
uses JDBC drivers to connect with the database. There are four types of JDBC drivers:
We can use JDBC API to access tabular data stored in any relational database. By the
help of JDBC API, we can save, update, delete and fetch data from the database. It is
like Open Database Connectivity (ODBC) provided by Microsoft.
We can use JDBC API to handle database using Java program and can perform the
following activities:
ORM and there is no underlying code for the implementation. Spring Data JPA
is part of the spring framework. The goal of spring data repository abstraction
is to significantly reduce the amount of boilerplate code required to implement
a data access layer for various persistence stores. Spring Data JPA is not a
JPA provider, it is a library/framework that adds an extra layer of abstraction
on the top of our JPA provider line Hibernate.
a relational database (tables and records) and Java application (classes and objects).
In the following figure, the ORM layer is an adapter layer. It adapts the language of
object graphs to the language of SQL and relation tables.
The ORM layer exists between the application and the database. It converts the Java classes
and objects so that they can be stored and managed in a relational database. By default, the
name that persists become the name of the table, and fields become columns. Once an
application sets-up, each table row corresponds to an object.
https://www.javatpoint.com/spring-boot-jpa
https://www.geeksforgeeks.org/spring-boot-spring-data-jpa/
Default Configuration
• a dependency on the HikariCP(Mysql) connection pool and a
basic default configuration. You can set all of HikariCP’s
configuration parameters in your application.properties file by
adding the prefix spring.datasource.hikari to the parameter
name.
• Most enterprise applications use a standalone database, e.g.,
a PostgreSQL or Oracle database server. In that case, you only
need to provide the URL, user name, and password to connect
to that database. You can do that by setting the following 3
configuration properties in your application.properties file.
• spring.datasource.url=jdbc:postgresql:
//localhost:5432/test
• spring.datasource.username=postgres
• spring.datasource.password=postgres
spring.data.jpa.repositories.enabled=false
Development Configuration
To enable you to understand how your application interacts with
the database and find performance issues before deploying them to
production.
logging.level.org.hibernate=INFO
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.cache=DEBUG
logging.level.org.hibernate.stat=DEBUG
Production Configuration
In production, you should set Hibernate’s log level to ERROR to
keep the overhead as small as possible.
logging.level.org.hibernate=ERROR
Initial Setup
First, let’s say we have a Product entity as our domain class
@Entity
public class Product {
@Id
private long id;
private String name;
private double price;
// constructors, getters and setters
}
Each of our Product instances has a unique identifier: id, its name and
its price associated with it.
To access our Products, we’ll need a ProductRepository:
public interface ProductRepository extends
PagingAndSortingRepository<Product, Integer> {
List<Product> findAllByPrice(double price, Pageable
pageable);
}
By having it extend PagingAndSortingRepository, we
get findAll(Pageable pageable) and findAll(Sort sort) methods for
paging and sorting.
Conversely, we could have chosen to extend JpaRepository instead, as it
extends PagingAndSortingRepository too.
Once we extend PagingAndSortingRepository, we can add our own
methods that take Pageable and Sort as parameters, like we did here
with findAllByPrice.
Let’s take a look at how to paginate our Products using our new method.
Pagination
Once we have our repository extending from PagingAndSortingRepository,
we just need to:
Entity - Entity.java
package com.tutorialspoint.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table
@NamedQuery(name = "Employee.findByEmail",
query = "select e from Employee e where e.email = ?1")
public class Employee {
@Id
@Column
private int id;
@Column
private String name;
@Column
private int age;
@Column
private String email;
package com.tutorialspoint.repository;
import
org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.entity.Employee;
@Repository
public interface EmployeeRepository extends
CrudRepository<Employee, Integer> {
public List<Employee> findByName(String name);
public List<Employee> findByAge(int age);
public Employee findByEmail(String email);
}
package com.tutorialspoint;
import java.util.List;
import javax.sql.DataSource;
/**
* This is the method to be used to create
* a record in the Student and Marks tables.
*/
public void create(String name, Integer age, Integer
marks, Integer year);
/**
* This is the method to be used to list down
* all the records from the Student and Marks
tables.
*/
public List<StudentMarks> listStudents();
}
package com.tutorialspoint;
id = "txAdvice" transaction-manager =
<tx:advice
"transactionManager">
<tx:attributes>
<tx:method name = "create"/>
</tx:attributes>
</tx:advice>
https://climbtheladder.com/10-spring-data-jpa-best-
practices/
https://medium.com/@eidan.khan659/how-to-optimize-
performance-with-spring-data-jpa-c615db786f7