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
@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
CompteClient clientToCompteClient(Client client);
CAN YOU HELP ME PLEASE TO RESOLVE THIS PROBLEM ??!!
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
Most helpful comment
As Andreas is saying, all you need is reversing the logic of that qualified method and let it create an
ArrayListfrom a single string.Or you use an inline expression, e.g. like so:
I'm going to close this one as "Not a bug".