Bean Persistence

Ldaptive provides an API similar to the J2EE entity manager to facilitate reading and writing LDAP data with Java beans. This support is provided in a separate library that is available in the jars directory of the latest download.

Or included as a maven dependency:

 <dependencies>
  <dependency>
    <groupId>org.ldaptive</groupId>
    <artifactId>ldaptive-beans</artifactId>
    <version>2.3.2</version>
  </dependency>
</dependencies>

The interface for ldap entry manager looks like:

public interface LdapEntryManager<T>
{
  T find(T object) throws LdapException;

  Response<Void> add(T object) throws LdapException;

  Response<Void> merge(T object) throws LdapException;

  Response<Void> delete(T object) throws LdapException;
}

DefaultLdapEntryManager

Ldaptive provides a default implementation of the LdapEntryManager which delegates to the appropriate add, merge, or delete operation.

Simple Java bean to illustrate usage:

@Entry(
  dn = "distinguishedName"
  attributes = {
    @Attribute(name = "displayName", property = "name"),
    @Attribute(name = "mail", property = "email"),
    @Attribute(name = "telephoneNumber", property = "phoneNumber"),
  })
public class MyObject
{
  private String distinguishedName;
  private String name;
  private String email;
  private String phoneNumber;

  public MyObject() {}

  public MyObject(String dn) { distinguisedName = dn; }

  public String getDistinguishedName() { return distinguishedName; }

  public String getName() { return name; }

  public void setName(String s) { name = s; }

  public String getEmail() { return email; }

  public void setEmail(String s) { email = s; }

  public String getPhoneNumber() { return phoneNumber; }

  public void setPhoneNumber(String s) { phoneNumber = s; }
}

Perform some common operations against the LDAP:

DefaultLdapEntryManager<MyObject> manager = new DefaultLdapEntryManager<MyObject>(
  new DefaultLdapEntryMapper<MyObject>(), new DefaultConnectionFactory("ldap://directory.ldaptive.org"));

// add a new entry to the LDAP
MyObject addObject = new MyObject("uid=dfisher,ou=people,dc=ldaptive,dc=org");
addObject.setName("Daniel Fisher");
addObject.setEmail("dfisher@ldaptive.org");
manager.add(addObject);

// modify the entry in the LDAP
MyObject mergeObject = manager.find(new MyObject("uid=dfisher,ou=people,dc=ldaptive,dc=org"));
mergeObject.setPhoneNumber("555-555-1234");
manager.merge(mergeObject);