• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 496

15 Nov 2025 05:33PM UTC coverage: 72.192% (-0.008%) from 72.2%
496

push

github

web-flow
Merge pull request #412 from hazendaz/renovate/major-spring-core

Update spring core to v7 (major)

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11922 of 16018 relevant lines covered (74.43%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.7
/samples/tutorial/src/main/java/tutorial/persistence/Database.java
1
/*
2
 * MIT License
3
 * Copyright (c) 2006-2025 JMockit developers
4
 * See LICENSE file for full license text.
5
 */
6
package tutorial.persistence;
7

8
import jakarta.persistence.EntityManager;
9
import jakarta.persistence.EntityManagerFactory;
10
import jakarta.persistence.Persistence;
11
import jakarta.persistence.PersistenceException;
12
import jakarta.persistence.Query;
13

14
import java.sql.SQLException;
15
import java.util.List;
16

17
/**
18
 * This class is a <em>static facade</em> for persistence operations. All methods are <code>static</code>, so it can be
19
 * statically imported in client classes for maximum usability.
20
 * <p>
21
 * All of the persistence operations made available through this facade access a thread-bound <em>persistence
22
 * context</em>. In this particular implementation, the standard <strong>JPA</strong> API is used, where
23
 * <code>jakarta.persistence.EntityManager</code> represents a work unit. Typically, each work unit instance exists only
24
 * long enough to perform a single database transaction. Transaction demarcation is not a responsibility of this class,
25
 * however, which simply keeps the association between the current thread and a dedicated work unit object (an
26
 * <code>EntityManager</code> instance). (In a web app, the persistence context can be tied to the HTTP request/response
27
 * cycle, which normally runs entirely in a single thread for each request/response pair; a central action-dispatch
28
 * servlet can commit or rollback the current transaction, while a custom <code>jakarta.servlet.Filter</code> can close
29
 * the thread-bound <code>EntityManager</code>.)
30
 * <p>
31
 * Compared to direct use of an ORM API such as JPA, or to the use of <em>Data Access Objects</em> (the "DAO" pattern),
32
 * this <em>static persistence facade</em> pattern has several advantages. Mainly, client code which needs to perform
33
 * high-level persistence operations (such as persisting a new entity instance, deleting an already persisted entity, or
34
 * finding and loading persistent entities) tends to be simpler and shorter. In the case of entity-specific DAO classes,
35
 * potentially thousands of lines of code are saved, without any real loss in portability (the facade implementation can
36
 * adapt to new versions of the ORM API or even a different ORM API; and consider that switching between an ORM API and
37
 * plain use of JDBC is not viable anyway, since with JDBC the DAO classes need to expose methods for the execution of
38
 * "UPDATE" statements, which are not needed with modern ORM APIs).
39
 */
40
public final class Database {
41
    private static final ThreadLocal<EntityManager> workUnit = new ThreadLocal<>();
1✔
42
    private static final EntityManagerFactory entityManagerFactory;
43

44
    static {
45
        EntityManagerFactory factory = null;
1✔
46

47
        try {
48
            factory = Persistence.createEntityManagerFactory("AppPersistenceUnit");
1✔
49
        } catch (PersistenceException e) {
×
50
            e.printStackTrace();
×
51
        }
1✔
52

53
        entityManagerFactory = factory;
1✔
54
    }
1✔
55

56
    private Database() {
57
    }
58

59
    public static <E> E find(Class<E> entityClass, Object entityId) {
60
        return workUnit().find(entityClass, entityId);
×
61
    }
62

63
    public static <E> List<E> find(String ql, Object... args) {
64
        Query query = workUnit().createQuery(ql);
1✔
65
        int position = 1;
1✔
66

67
        for (Object arg : args) {
1✔
68
            query.setParameter(position, arg);
1✔
69
            position++;
1✔
70
        }
71

72
        try {
73
            return query.getResultList();
1✔
74
        } catch (PersistenceException e) {
×
75
            Throwable cause = e.getCause();
×
76
            throw cause instanceof SQLException ? new RuntimeException(cause) : e;
×
77
        }
78
    }
79

80
    public static void persist(Object data) {
81
        workUnit().persist(data);
1✔
82
    }
1✔
83

84
    public static void remove(Object persistentEntity) {
85
        workUnit().remove(persistentEntity);
×
86
    }
×
87

88
    private static EntityManager workUnit() {
89
        EntityManager wu = workUnit.get();
1✔
90

91
        if (wu == null) {
1✔
92
            wu = entityManagerFactory.createEntityManager();
1✔
93
            workUnit.set(wu);
1✔
94
        }
95

96
        return wu;
1✔
97
    }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc