-
Notifications
You must be signed in to change notification settings - Fork 14
/
NamedQueryManager.java
43 lines (38 loc) · 1.3 KB
/
NamedQueryManager.java
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
package cz.cvut.kbss.jopa.query;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Manages named queries in the persistence unit.
*/
public class NamedQueryManager {
private final Map<String, String> queryMap = new HashMap<>();
/**
* Adds a named query mapping.
*
* @param name Named of the query
* @param query Query string
* @throws IllegalArgumentException If there already exists a mapping for the specified name
*/
public void addNamedQuery(String name, String query) {
Objects.requireNonNull(name);
Objects.requireNonNull(query);
if (queryMap.containsKey(name)) {
throw new IllegalArgumentException("Query with name " + name + " already exists in this persistence unit.");
}
queryMap.put(name, query);
}
/**
* Gets a query mapped by the specified name.
*
* @param name Query name
* @return Query string
* @throws IllegalArgumentException If a query has not been defined with the given name
*/
public String getQuery(String name) {
if (!queryMap.containsKey(name)) {
throw new IllegalArgumentException("Query with name " + name + " was not found in this persistence unit.");
}
return queryMap.get(name);
}
}