Argument type mismatch with BeanUtils.copyProperties

dto object:

public class DTOUser implements UserDetails {
/**
 * 
 */
private static final long serialVersionUID = -769714837633005963L;
private Long id;
private String account;
private String password;
private String statusStr;
private UserStatus status;
private int systemAdmin;
private long operator;
private String operateTime;
private String name;
private String company;
private String email;
private String telephone;
private List<DTOAuthority> dtoAuthorities = new ArrayList<DTOAuthority>();
private List<DTOAgreement> dtoAgreements = new ArrayList<DTOAgreement>();}

      

an object:

@Entity
@Table(name="adt_user")
public class User {
private long id;
private String account;
private String password;
private String statusStr;
private UserStatus status;
private int systemAdmin;
private long operator;
private String operateTime;
private String name;
private String company;
private String email;
private String telephone;
private Set<Authority> authorities = new HashSet<Authority>();
private Set<Agreement> agreements = new HashSet<Agreement>();}

      

I am using the following method to copy a value to an entity, but there is an exception:

java.lang.IllegalArgumentException: Cannot invoke com.hna.adt.orm.User.setAuthorities - argument type mismatch

what's wrong with him?

BeanUtils.copyProperties(entity, value);

      

0


source to share


2 answers


You have

private List<DTOAuthority> dtoAuthorities = new ArrayList<DTOAuthority>();

      

against.

private Set<Authority> authorities = new HashSet<Authority>();

      

If BeanUtils.copyProperties

only considers setters and item getters and is not smart enough to figure out that Set

both List

are collections and iterations and copy items one by one, which still requires to be Authority

compatible with DTOAuthority

- then it will throw this reflection error, then Set

cannot be attributed to List

, that is, incompatible.



If you try to do the same at compile time

entity.setAuthorities(value.getDtoAuthorities());

      

Then you get the same message as the compilation error.

You must either change dtoAuthorities

to Set

or authorities

how List

.

0


source


If you check the type of authority and agreements, they do not match the definition of another class. For beanutils to work correctly, make sure the attribute type matches.



+1


source







All Articles