@Override
 public Serializable getReference(Object entity) throws IllegalStateException {
   checkConfig();
   DocumentModel entry = null;
   if (entity != null) {
     if (entity instanceof DirectoryEntry) {
       entry = ((DirectoryEntry) entity).getDocumentModel();
     } else if (entity instanceof DocumentModel) {
       entry = (DocumentModel) entity;
     }
     if (entry != null) {
       if (!entry.hasSchema(schema)) {
         return null;
       }
       String result = (String) entry.getProperty(schema, idField);
       if (hierarchical) {
         String parent = (String) entry.getProperty(schema, parentField);
         try (Session session = directory.getSession()) {
           while (parent != null) {
             entry = session.getEntry(parent);
             if (entry == null) {
               break;
             }
             result = parent + separator + result;
             parent = (String) entry.getProperty(schema, parentField);
           }
         }
       }
       return result;
     }
   }
   return null;
 }
 @Override
 public Object fetch(Object value) throws IllegalStateException {
   checkConfig();
   if (value != null && value instanceof String) {
     String id = (String) value;
     if (hierarchical) {
       String[] ids = StringUtils.split(id, separator);
       if (ids.length > 0) {
         id = ids[ids.length - 1];
       } else {
         return null;
       }
     }
     try (Session session = directory.getSession()) {
       DocumentModel doc = session.getEntry(id);
       if (doc != null) {
         return new DirectoryEntry(directory.getName(), doc);
       }
       return null;
     }
   }
   return null;
 }