-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjdbc-vs-jpa.json
More file actions
54 lines (54 loc) · 2.53 KB
/
jdbc-vs-jpa.json
File metadata and controls
54 lines (54 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
"id": 100,
"slug": "jdbc-vs-jpa",
"title": "JDBC versus JPA",
"category": "enterprise",
"difficulty": "intermediate",
"jdkVersion": "11",
"oldLabel": "Java EE",
"modernLabel": "Jakarta EE 8+",
"oldApproach": "JDBC",
"modernApproach": "JPA EntityManager",
"oldCode": "String sql = \"SELECT * FROM users WHERE id = ?\";\ntry (Connection con = dataSource.getConnection();\n PreparedStatement ps =\n con.prepareStatement(sql)) {\n ps.setLong(1, id);\n ResultSet rs = ps.executeQuery();\n if (rs.next()) {\n User u = new User();\n u.setId(rs.getLong(\"id\"));\n u.setName(rs.getString(\"name\"));\n }\n}",
"modernCode": "@PersistenceContext\nEntityManager em;\n\npublic User findUser(Long id) {\n return em.find(User.class, id);\n}\n\npublic List<User> findByName(String name) {\n return em.createQuery(\n \"SELECT u FROM User u WHERE u.name = :name\",\n User.class)\n .setParameter(\"name\", name)\n .getResultList();\n}",
"summary": "Replace verbose JDBC boilerplate with JPA's object-relational mapping and EntityManager.",
"explanation": "JPA (Jakarta Persistence API) maps Java objects to database rows, eliminating manual ResultSet processing and SQL string concatenation. EntityManager provides find(), persist(), and JPQL queries so you work with domain objects instead of raw SQL, while the container manages connection pooling and transactions.",
"whyModernWins": [
{
"icon": "🗺️",
"title": "Object mapping",
"desc": "Entities are plain annotated classes — no manual ResultSet-to-object translation."
},
{
"icon": "🔒",
"title": "Type-safe queries",
"desc": "JPQL operates on entity types and fields rather than raw table and column strings."
},
{
"icon": "⚡",
"title": "Built-in caching",
"desc": "First- and second-level caches reduce database round-trips automatically."
}
],
"support": {
"state": "available",
"description": "Widely available since Jakarta EE 8 / Java 11"
},
"prev": "enterprise/ejb-vs-cdi",
"next": "enterprise/jpa-vs-jakarta-data",
"related": [
"enterprise/servlet-vs-jaxrs",
"enterprise/ejb-vs-cdi",
"io/try-with-resources-effectively-final"
],
"docs": [
{
"title": "Jakarta Persistence Specification",
"href": "https://jakarta.ee/specifications/persistence/"
},
{
"title": "Jakarta Persistence 3.1 API",
"href": "https://jakarta.ee/specifications/persistence/3.1/apidocs/"
}
]
}