Search code examples
javaspringldap

How to bind multi-valued attributes in LDAP using Spring repository


I want to retrieve a multi-valued attribute from LDAP using a Spring repository.

My actual code is something like this:

@Entry(objectClasses = { "class1", "class2" }, base="ou=my_base_dn")
public final class MyLdapEntity {

    @Id
    private Name dn;

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

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

    @Attribute(name="multiValuedAttr")
    private String[] multiValuedAttr;
}

And my repository class:

@Repository
public interface MyLdapRepository extends CrudRepository<MyLdapEntity, Long> {
}

And when I try to get the multivalued attr I can do it only with the first entry.

¿Is there any way to do this mapping with an annotation?


Solution

  • Solved.

    Instead of declaring the multiValuedAttr with String[] it must be defined as List<String>

    @Entry(objectClasses = { "class1", "class2" }, base="ou=my_base_dn")
    public final class MyLdapEntity {
    
        @Id
        private Name dn;
    
        @Attribute(name="name")
        private String name;
    
        @Attribute(name="sureName")
        private String sureName;
    
        @Attribute(name="multiValuedAttr")
        private String[] multiValuedAttr;
    }
    

    Maybe cause List<String> has methods to add elements without increment the array size explicitly.