What is java database connectivity(JDBC)
JDBC uses classes and attributes to manipulate the database, but the
content of the database is not manipulated as objects and attribute
What are the pros of java database connectivity
o database accessible from Java
o handle errors with Exception handling
What are the cons of java database connectivity
o SQL statements in the code
o complexify the program
o mapping between object and tables needs to be specified and
maintained
What is Object-Relational Impedance Mismatch
Object-Relational Impedance Mismatch’ is just a fancy way of saying that
object models and relational models do not work very well together.
RDBMSs represent data in a tabular format , whereas object-oriented
languages, such as Java, represent it as an interconnected graph of objects.
Loading and storing graphs of objects using a tabular relational database
exposes us to […] mismatch problems.
What is object relational mapping(ORM)
ORM = a task
- converting OO concepts to relational databases concepts
- input: a domain model (class diagram)
- output: relational schema
How do we specify and use our mapping in our java application?
By use of Jakarta and hibernate
what is jakarta persistemce API(JPA)
▪ specification (not a tool -> guidelines)
▪ removes the burden of thinking relationally in Java code
What is hibernate
It’s the implementation of JPA (= JPA provider)
It rely on ORM to simplify the persistence layer
▪ e.g., manipulating data as objects, without JDBC or SQ
What is the purpose of JPA (Jakarta Persistence API)?
JPA simplifies the persistence layer by allowing developers to manage relational data as Java objects using annotations, an EntityManager API, and JPQL (object-based query language)
Is JPA a tool or a specification?
JPA is a specification (guidelines). Tools like Hibernate provide its implementation
What is Hibernate in relation to JPA?
Hibernate is a JPA provider (implementation). It manages ORM and allows data to be manipulated as Java objects, removing the need for JDBC/SQL code
What annotation makes a class persistable in JPA?
@Entity
How do you mark the primary key in JPA?
With @Id. Optionally, use @GeneratedValue to auto-generate unique primary keys
Which JPA annotations handle associations?
One-to-Many: @OneToMany / @ManyToOne
Many-to-Many: @ManyToMany
What is the EntityManager API in JPA?
An API that provides CRUD operations (Create, Read, Update, Delete) without writing JDBC or SQL code
What is JPQL, and how does it differ from SQL?
JPQL (Java Persistence Query Language) is like SQL but operates on objects, attributes, and relationships, not on tables and columns
Give an example JPQL query.
SELECT s.name FROM Student s
SELECT o FROM Offering o WHERE o.courseID = “ECSE321”
What are the main steps of using JPA persistence?
1.Annotate classes for persistence (@Entity, @Id, etc.).
2.Manage data with EntityManager (CRUD).
3.Query with JPQL
What is Spring Data JPA?
A framework built on top of JPA that reduces boilerplate by providing repositories and auto-generated query methods
What does CrudRepository provide in Spring Data JPA?
Standard CRUD methods: save, findOne, findAll, count, delete, exists
What are the two ways to define query methods in Spring Data JPA?
1.With @Query annotation (using JPQL).
2.Automatically derived from method names (e.g., findByLastOrFirst)
Give an example of a custom query with @Query.
@Query(“SELECT o FROM Offering o JOIN o.course c WHERE c.courseId = ?1”)
Collection<Offering> findAllOfferingOfCourse(String courseId);</Offering>
What problem does ORM (and JPA/Hibernate) solve compared to JDBC?
It solves the object-relational impedance mismatch: the difficulty of mapping object-oriented models (Java) to relational models (tables)