DEV Community

shantanu mahakale
shantanu mahakale

Posted on

Quick Recap: JPA (Java Persistence API)

JPA is a standard in Java for object-relational mapping (ORM) — it allows developers to map Java objects to database tables and perform CRUD operations without writing SQL manually.

It is mainly used with relational databases (e.g., Oracle, PostgreSQL, MySQL) and can also be used with MongoDB using Spring Data.


Core Concepts in JPA

  • Entity → Represents a table row
  • Entity Manager → Handles DB operations
  • Persistence Context → Cache/session for entities
  • Repository → Interface for CRUD queries
  • Transaction → Ensures atomic operations

Mapping Entities (Oracle Example)

@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
Enter fullscreen mode Exit fullscreen mode

JPA Annotations Cheat Sheet

Annotation Purpose
@Entity Marks a class as a DB entity
@Table Maps to DB table
@Id Primary key
@GeneratedValue Auto-increment strategy
@Column Map field to column
@OneToMany Relationship mapping
@ManyToOne Foreign key relation
@Transactional Wrap method in transaction

Spring Data JPA Repository (Oracle Example)

public interface UserRepo extends JpaRepository<User, Long> {
List<User> findByName(String name);
}
Enter fullscreen mode Exit fullscreen mode

👉 No SQL needed — method names generate queries.


Transactions (Relational DBs)

@Transactional
public void saveUser(User user) {
repo.save(user);
}
Enter fullscreen mode Exit fullscreen mode

👉 Guarantees ACID behavior with Oracle/Postgres/MySQL.


JPA with MongoDB (Spring Data)

MongoDB is NoSQL, so traditional JPA is not used. Instead, we use Spring Data MongoDB, which follows a similar repository pattern.

Entity for MongoDB:

@Document(collection = "users")
public class User {
@Id
private String id;
private String name;
}
Enter fullscreen mode Exit fullscreen mode

Repository:

public interface UserRepo extends MongoRepository<User, String> {}
Enter fullscreen mode Exit fullscreen mode

👉 Still works like JPA, but no joins, no relationships, no transactions across collections.


JPA vs MongoDB – Key Differences

Feature JPA (Oracle) MongoDB
Database Type Relational (SQL) NoSQL (Document)
Schema Fixed Flexible
Transactions ACID Limited (single doc)
Joins Yes No (embed instead)
Querying JPQL / Criteria Mongo Query DSL
Repository JpaRepository MongoRepository

When to Use JPA (Relational)

✔ Complex relationships

✔ Transactions required

✔ Strong schema & constraints

✔ Enterprise applications

Examples: Oracle, PostgreSQL, MySQL


When to Use MongoDB (NoSQL)

✔ Flexible schema

✔ High scalability

✔ JSON-style data

✔ Fast reads/writes

✔ No complex joins needed

Examples: Logs, user profiles, IoT data, analytics


Summary

Use Case Recommended
Banking / Finance JPA with Oracle
Microservices JPA + PostgreSQL OR MongoDB
IoT / Analytics MongoDB
High reads/writes MongoDB
Strict schema JPA

Top comments (0)