• 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

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

8
import edu.umd.cs.findbugs.annotations.NonNull;
9
import edu.umd.cs.findbugs.annotations.Nullable;
10

11
import jakarta.persistence.EntityManager;
12
import jakarta.persistence.PersistenceContext;
13
import jakarta.persistence.Query;
14
import jakarta.transaction.Transactional;
15

16
import java.util.List;
17

18
import org.checkerframework.checker.index.qual.NonNegative;
19

20
/**
21
 * Provides access to the application database, allowing transient instances of entity classes to be persisted, and
22
 * persistent instances to be recovered or removed from the database.
23
 */
24
@Transactional
25
public class Database {
1✔
26
    @PersistenceContext
27
    private EntityManager em;
28

29
    /**
30
     * Finds an entity in the application database given its class and unique id.
31
     *
32
     * @param <E>
33
     *            the element type
34
     * @param entityClass
35
     *            the entity class
36
     * @param id
37
     *            the id
38
     *
39
     * @return the persistent entity if found, or <code>null</code> if not found
40
     */
41
    @Nullable
42
    public <E extends BaseEntity> E findById(@NonNull Class<E> entityClass, int id) {
43
        return em.find(entityClass, id);
1✔
44
    }
45

46
    /**
47
     * Finds one or more persistent entities of a certain type in the application database.
48
     *
49
     * @param <E>
50
     *            the element type
51
     * @param qlStatement
52
     *            a JPQL "select" statement that locates entities of the same type
53
     * @param qlArgs
54
     *            zero or more argument values for the positional query parameters specified in the JPQL statement, in
55
     *            the same order as the parameter positions
56
     *
57
     * @return the list of zero or more entities found, in an arbitrary order or in the order specified by an "order by"
58
     *         clause (if any)
59
     *
60
     * @see #find(int, String, Object...)
61
     */
62
    @NonNull
63
    public <E extends BaseEntity> List<E> find(@NonNull String qlStatement, @NonNull Object... qlArgs) {
64
        return find(0, qlStatement, qlArgs);
1✔
65
    }
66

67
    /**
68
     * Finds one or more persistent entities of a certain type in the application database, up to a given maximum number
69
     * of entities.
70
     *
71
     * @param <E>
72
     *            the element type
73
     * @param maxResults
74
     *            the maximum number of resulting entities to be returned, or <code>0</code> if there is no limit
75
     * @param qlStatement
76
     *            a JPQL "select" statement that locates entities of the same type
77
     * @param qlArgs
78
     *            zero or more argument values for the positional query parameters specified in the JPQL statement, in
79
     *            the same order as the parameter positions
80
     *
81
     * @return the list of zero or more entities found, in an arbitrary order or in the order specified by an "order by"
82
     *         clause (if any)
83
     */
84
    @NonNull
85
    public <E extends BaseEntity> List<E> find(@NonNegative int maxResults, @NonNull String qlStatement,
86
            @NonNull Object... qlArgs) {
87
        Query query = em.createQuery(qlStatement);
1✔
88

89
        if (maxResults > 0) {
1✔
90
            query.setMaxResults(maxResults);
1✔
91
        }
92

93
        for (int i = 0; i < qlArgs.length; i++) {
1✔
94
            query.setParameter(i + 1, qlArgs[i]);
1✔
95
        }
96

97
        return query.getResultList();
1✔
98
    }
99

100
    /**
101
     * Saves the state of a given entity to the application database, whether it is new (still transient, with no id) or
102
     * already persisted (with an id).
103
     * <p>
104
     * In either case, the persistence context is synchronized to the application database, so that any pending
105
     * "inserts", "updates" or "deletes" get executed at this time.
106
     *
107
     * @param entity
108
     *            the entity
109
     */
110
    public void save(@NonNull BaseEntity entity) {
111
        if (entity.isNew()) {
1✔
112
            em.persist(entity);
1✔
113
        } else if (!em.contains(entity)) { // in case it is a detached entity
1!
114
            em.merge(entity);
×
115
        }
116

117
        em.flush();
1✔
118
    }
1✔
119

120
    /**
121
     * Removes a given persistent entity from the application database.
122
     *
123
     * @param entity
124
     *            the entity
125
     */
126
    public void remove(@NonNull BaseEntity entity) {
127
        em.remove(entity);
×
128
    }
×
129
}
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