Java Reference
In-Depth Information
<phones id="${db:id('Telephone')}" user_id="${db:id('User')}" type="0"
number="481 516-2342"/>
</dataset>
Instead of using hardcoded ID s for each class (like ${id} and ${phoneId} ), this new
version uses a generic ${db:id('ClassName')} function, which is much more flexi-
ble. The db:id() function then returns the respective ID for a given class, which is
either the value defined by the Hibernate listener or 1 by default (which means no
entity has been generated by JPA for that class. In this case the value doesn't matter,
but using 1 makes it easier to debug in case of errors). To add support for this func-
tion, first we need to change ELContextImpl to support function mapping, as shown
in listing 18.12.
Listing 18.12
Changes to ELContextImpl to support the id function
[...]
public final class ELContextImpl extends ELContext {
[...]
private FunctionMapper functionMapper = new ELFunctionMapperImpl();
[...]
@Override
public FunctionMapper getFunctionMapper() {
return functionMapper;
}
[...]
}
The ID function itself is defined at ELFunctionMapperImpl , shown in listing 18.13.
Listing 18.13
Custom EL functions ( ELFunctionMapperImpl )
[...]
public class ELFunctionMapperImpl extends FunctionMapper {
private static final Map<String, Method> METHODS =
new HashMap<String, Method>();
private static final Map<String,Long> IDS = new HashMap<String, Long>();
private static final int INITIAL_ID = 1;
B
C
static {
for ( Method method : ELFunctionMapperImpl. class .getDeclaredMethods() ) {
int modifiers = method.getModifiers();
String name = method.getName();
if ( Modifier.isStatic(modifiers) && name.startsWith("db_")) {
METHODS.put(name.replace('_', ':'), method);
}
}
}
@Override
public Method resolveFunction(String prefix, String localName) {
return METHODS.get(prefix+":"+localName);
}
D
 
 
Search WWH ::




Custom Search