Mapstruct: Can't map property "java.lang.String mail" to "java.util.ArrayList<java.lang.String> listMail"

Created on 13 Apr 2016  路  5Comments  路  Source: mapstruct/mapstruct

I have a client sources like below :

    @Entity
    @Table(name="client")
     public class Client implements Serializable {

         @Id
         @Column(name="code_client", nullable = false, length=10)
         private String codeClient;

         @Column(name="mail", length=100)
         private String mail;`
       } 

and target is :

 public class CompteClient{ 

    private String codeClient;
    private ArrayList<String> listMail;
 }

The mapper class :

@Mapper
 public interface ClientMapper {

public static ClientMapper INSTANCE = Mappers.getMapper(ClientMapper.class );

 @Mappings({
     @Mapping(source="codeClient", target="codeClient"),
     @Mapping(source="mail",qualifiedBy=FirstElement.class, target="listMail"
})
CompteClient clientToCompteClient(Client client);`

To map a mail (string) to listMail(List) i need set mail to the first element of listMail, i have created an annotation FirstElement like :

       @Qualifier
       @Target(ElementType.METHOD)
       @Retention(RetentionPolicy.SOURCE)
       public @interface FirstElement {
     }

and the classe where i can set to the first element :
public class IterableNonInterableUtil {

   @FirstElement
    public <T> T first( List<T> in ) {
         if ( in != null && !in.isEmpty() ) {
           return in.get( 0 );
        }
        else {
             return null;
        }
   }

`

when i run the maven command : generate-sources, i have this error message :

[ERROR] diagnostic: C:\Users\mameharzi\Documents\Sap workspace\mblog-services-sap\src\main\java\fr\rs2i\mblog\sap\mapper\ClientMapper.java:33: error: Can't map property "java.lang.String mail" to "java.util.ArrayList listMail". Consider to declare/implement a mapping method: "java.util.ArrayList map(java.lang.String value)".
CompteClient clientToCompteClient(Client client);

CAN YOU HELP ME PLEASE TO RESOLVE THIS PROBLEM ??!!

not a bug

Most helpful comment

As Andreas is saying, all you need is reversing the logic of that qualified method and let it create an ArrayList from a single string.

Or you use an inline expression, e.g. like so:

@Mapping(target = "listMail", expression = "java(Collections.singletonList(client.getMail()))")

I'm going to close this one as "Not a bug".

All 5 comments

Well, you're trying to map from String to ArrayList<String>, so you'll have to provide a method that takes String as an argument and returns an ArrayList<String>. Your method first does the opposite of that... :smile:

One more note: When creating @Qualifier annotations, better use @Retention(RetentionPolicy.CLASS) - there are several cases where SOURCE won't be enough. We tried to fix that in our latest versions of documentations and examples and hopefully found all spots.

As Andreas is saying, all you need is reversing the logic of that qualified method and let it create an ArrayList from a single string.

Or you use an inline expression, e.g. like so:

@Mapping(target = "listMail", expression = "java(Collections.singletonList(client.getMail()))")

I'm going to close this one as "Not a bug".

Think you for your help

Unsatisfied by the explanation

@lo-rodriguez you are commenting on a 4 year old issue which has been answered and the explanation has been accepted by the original poster.

If something is not clear I would suggest that you create a Stackoverflow question or write in the Gitter room

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PFStruct picture PFStruct  路  5Comments

jrcacd picture jrcacd  路  4Comments

lordplagus02 picture lordplagus02  路  5Comments

imtiazShakil picture imtiazShakil  路  4Comments

pe-st picture pe-st  路  6Comments