Java Reference
In-Depth Information
E
public static long
db_id(String className) {
long
id;
if
( IDS.containsKey(className) ) {
id = IDS.get(className);
}
else
{
id = INITIAL_ID;
}
return
id;
}
F
public static void
setId(String className,
long
newId) {
if
( ! IDS.containsKey(className) ) {
IDS.put(className, newId);
}
}
G
public static void
resetIds() {
IDS.clear();
}
H
public static long
getId(Class<?> clazz) {
return db_id(clazz.getSimpleName());
}
}
This defines the value returned by the
ID
function when no
ID
was set for a given class.
This initializes the map of static methods that define
EL
functions. To facilitate the
mapping, these methods starts with
db_
.
resolveFunction()
is part of the
EL
API
and is used to map an
EL
function (such as
db:id
) to a Java method (
db_id()
, in this case). It returns the method defined at
C
.
This is the
id
function properly speaking, which returns either the initial
ID
or the
value present in the
ID
's map.
Our custom Hibernate listener calls this method every time a new entity is generated,
but only the first value is saved in the
ID
's map; the other values are calculated by the
dataset, using this base
ID
when necessary (such as
${db:id('Telephone')+1}
, if the
dataset has two telephone rows). Notice that this mechanism assumes
ID
s are gener-
ated in sequence, which is true most of the time, but not always. If that doesn't apply
in your case (for instance, if you're using a generation strategy based on
UUID
s),
you'll need a different (and more complex) approach to represent multiple
ID
s, like
using a sequence parameter in the
id
function:
${db:id('Telephone',1)}
would
return the first generated
ID
,
${db:id(´Telephone',2)}
would return the second,
and so on. Then instead of keeping a single
ID
in the map, you'd keep a list of the gen-
erated
ID
s for each class.
The
ID
map must be reset before each method, so load methods always start with
id=1
(or whatever value is defined at
B
).
This is the helper method used by test cases that need to know the base
ID
for a
given class.
B
C
D
E
F
G
H