Ilya Zinkovich
Ilya Zinkovich

Reputation: 4410

LdapRepository update spring-ldap

Spring LdapRepository save() method throws exception when I'm trying to update an existing object in LDAP database.

org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException: ERR_250_ENTRY_ALREADY_EXISTS

What method should I use to update existing ldap objects?

Person class:

@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
public class Person implements Serializable {

public Person() {
}

@Id
private Name dn;

@Attribute(name = "cn")
@DnAttribute(value = "cn")
@JsonProperty("cn")
private String fullName;

@Attribute(name = "uid")
private String uid;

private String mail;

@Attribute(name = "sn")
private String surname;
//setters and getters
}

Person repo interface:

public interface PersonRepo extends LdapRepository<Person> {
}

That's how I'm updating person:

personRepo.save(person);

Upvotes: 0

Views: 2101

Answers (1)

Zoran Regvart
Zoran Regvart

Reputation: 4690

Default implementation for Spring LDAP repositories is SimpleLdapRepository, that checks the property annotated with @Id to determine if the objects is new - and perform create, or old - and perform update.

I'm guessing that Person.dn is null when you're trying to perform update.

You also can take the control over this by implementing org.springframework.data.domain.Persistable and place your logic in the isNew() method.

See the implementation details.

Upvotes: 2

Related Questions