参数类型不匹配使用BeanUtils.copyProperties

时间:2013-09-28 07:02:30

标签: java

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>();}

实体:

@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>();}

我使用bellow方法将值复制到实体,但有一个例外:

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

它出了什么问题?

BeanUtils.copyProperties(entity, value);

2 个答案:

答案 0 :(得分:1)

如果检查权限和协议的类型,则它们与其他类定义不匹配。为了使beanutils正常工作,请确保属性类型匹配。

答案 1 :(得分:0)

你有

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

VS

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

如果BeanUtils.copyProperties只考虑元素的setter和getter,并且不够智能,无法确定SetList都是集合并迭代并逐个复制元素 - 仍然要求AuthorityDTOAuthority兼容 - 然后它会抛出此反射错误,然后Set无法从List分配,即不兼容。

如果您尝试在编译时执行相同的操作

entity.setAuthorities(value.getDtoAuthorities());

然后你会得到与编译错误相同的语句。

您应该将dtoAuthorities更改为Set或将authorities更改为List