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);
答案 0 :(得分:1)
如果检查权限和协议的类型,则它们与其他类定义不匹配。为了使beanutils正常工作,请确保属性类型匹配。
答案 1 :(得分:0)
你有
private List<DTOAuthority> dtoAuthorities = new ArrayList<DTOAuthority>();
VS
private Set<Authority> authorities = new HashSet<Authority>();
如果BeanUtils.copyProperties
只考虑元素的setter和getter,并且不够智能,无法确定Set
和List
都是集合并迭代并逐个复制元素 - 仍然要求Authority
与DTOAuthority
兼容 - 然后它会抛出此反射错误,然后Set
无法从List
分配,即不兼容。
如果您尝试在编译时执行相同的操作
entity.setAuthorities(value.getDtoAuthorities());
然后你会得到与编译错误相同的语句。
您应该将dtoAuthorities
更改为Set
或将authorities
更改为List
。