Previous | Next | Trail Map | Tips for LDAP Users | Miscellaneous

Renaming Objects

You use the Context.rename()(in the API reference documentation) method to rename an object in the directory. In the LDAP v2 this corresponds to the "modify RDN" operation that renames an entry within the same context (that is, renaming a sibling). In the LDAP v3 this corresponds to the "modify DN" operation, which is like "modify RDN", except that the old and new entries need not be in the same context. You can use Context.rename() to rename a leaf entry or an interior node. The following code renames an interior node from "ou=NewHires" to "ou=OldHires".
ctx.rename("ou=NewHires", "ou=OldHires");


Note: The Netscape Directory Server 4.1 does not support renaming interior nodes. If you run this example, you will get a ContextNotEmptyException(in the API reference documentation) .

Renaming to a Different Part of the DIT

With the LDAP v3, you can rename an entry to a different part of the DIT. To do this using Context.rename(), you must use a context that is the common ancestor for both the new and the old entries. For example, if you want to rename "cn=C. User, ou=NewHires, o=JNDITutorial" to "cn=C. User, ou=People, o=JNDITutorial", you must use the context named by "o=JNDITutorial". Here is an example that demonstrates this. If you try to run this example against an LDAP v2 server, you will get an InvalidNameException(in the API reference documentation) because version 2 does not support this feature.

ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People");

Note: The Netscape Directory Server 4.1 does not support renaming with different parent nodes. If you run this example using that server, you will get a CommunicationException(in the API reference documentation) (indicating a "protocol error").

Keeping the Old Name Attributes

In the LDAP, when you rename an entry, you have the option of keeping the entry's old relative distinguished name (RDN) as an attribute of the updated entry. For example, if you rename the entry "cn=C. User" to "cn=Claude User", you can specify whether you want the old RDN "cn=C. User" to be kept as an attribute.

To specify whether you want to keep the old name attribute when you use Context.rename(), use the "java.naming.ldap.deleteRDN" environment property. If this property's value is "true", the old RDN is removed. If its value is "false", the old RDN is kept as an attribute of the updated entry. The default is "true".

// Set property to keep RDN
env.put("java.naming.ldap.deleteRDN", "false");

// Create initial context
DirContext ctx = new InitialDirContext(env);

// Perform rename
ctx.rename("cn=C. User, ou=NewHires", "cn=Claude User,ou=NewHires");


Previous | Next | Trail Map | Tips for LDAP Users | Miscellaneous