在UnboundID LDAP SDK Persistence Framework中指定LDAPField的默认值



是否有一种(简单的)方法来指定LDAPField注释变量的默认值?例如,如果没有找到值,我不希望列表myNumericAttr为空,而是希望它保存一个空列表。

import com.unboundid.ldap.sdk.persist.LDAPField;
import com.unboundid.ldap.sdk.persist.LDAPObject;
@LDAPObject(structuralClass="myStructuralClass")
public class MyObject
{
    @LDAPField(attribute="myStringAttr")
    private String myStringAttr;
    @LDAPField(attribute="myNumericAttr")
    private List<Long> myNumericAttr;
}

作为一种解决方案,我可以自己实现postDecodeMethod,但这会导致大量的代码。

@SuppressWarnings("unused")
private void doPostDecode() throws LDAPPersistException, IllegalArgumentException, IllegalAccessException
{
    for (Field field : this.getClass().getDeclaredFields())
    {
        if(field.isAnnotationPresent(LDAPField.class))
        {
            // check if field value is null
            if (field.get(this) == null)
            {
                String fieldType = field.getType().getName();
                log.info("field type: {}", fieldType);
                if (fieldType.equals("java.lang.String"))
                {
                    field.set(this, "");
                }
                else if (fieldType.equals("java.util.List"))
                {
                    // Find out the type of list we are dealing with
                    ParameterizedType listGenericType = (ParameterizedType) field.getGenericType();
                    Class<?> listActualType = (Class<?>) listGenericType.getActualTypeArguments()[0];
                    log.debug("actual type of list: {}", listActualType.getName());
                    field.set(this, getModel(listActualType));
                }
            }
        }
    }
}
private <T> ArrayList<T> getModel(Class<T> type) {
    ArrayList<T> arrayList = new ArrayList<T>();
    return arrayList;
}

所以我的问题是,我错过了一些功能,还是实现自己的postDecodeMethod是目前唯一的可能性?

看一下@LDAPField注释类型的defaultDecodeValue元素。例如:

@LDAPField(attribute="myStringAttr",
           defaultDecodeValue="thisIsTheDefaultValue")
private String myStringAttr;

如果正在解码的条目中不存在myStringAttr属性,则持久性框架将以"thisIsTheDefaultValue"的值作为它存在的值。

最新更新