TypeMapping mapping;
String id;
if (entity instanceof ObjectKey) {
ObjectKey key = (ObjectKey) entity;
mapping = context.findTypeMapping(key.getType());
id = mapping.toIdString(key.getId());
} else {
mapping = context.findTypeMapping(entity);
id = mapping.getIdString(entity);
}