java.lang.IllegalArgumentException异常

时间:2011-08-15 07:13:29

标签: java netbeans

我在Netbeans中实现方法时遇到了错误。

java.lang.IllegalArgumentException: You have provided an instance of
an incorrect PK class for this find operation.
  Class expected : class java.lang.Integer,
  Class received : class java.lang.String.

以下是方法中使用的实体:

公共类Staff实现了Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)

@Column(name = "STAFF_ID")
private Integer staffId;
@Size(max = 30)
@Column(name = "NAME")
private String name;
@Size(max = 100)
@Column(name = "PASSWORD")
private String password;
@Size(max = 20)
@Column(name = "ROLES")
private String roles;

@Size(max = 60)
@Column(name = "EMAIL")
private String email;
@Size(max = 20)
@Column(name = "VERIFYSTATUS")
private String verifystatus;
@Size(max=10)
@Column(name = "VERIFYCODE")
private String verifycode;

public Staff() {
}

public Staff(Integer staffId) {
    this.staffId = staffId;
}

public Integer getStaffId() {
    return staffId;
}

public void setStaffId(Integer staffId) {
    this.staffId = staffId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getRoles() {
    return roles;
}

public void setRoles(String roles) {
    this.roles = roles;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getVerifyStatus() {
    return verifystatus;
}

public void setVerifyStatus(String verifystatus){
    this.verifystatus= verifystatus;
}

public String getVerifyCode() {
    return verifycode;
}
   public void setVerifyCode(String verifycode){
       this.verifycode= verifycode;
   }

@Override
public int hashCode() {
    int hash = 0;
    hash += (staffId != null ? staffId.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Staff)) {
        return false;
    }
    Staff other = (Staff) object;
    if ((this.staffId == null && other.staffId != null) || (this.staffId != null && !this.staffId.equals(other.staffId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.Staff[ staffId=" + staffId + " ]";
}

}

我的控制器中的方法:

public String verify(){
String result = "failed";
String authcode = staffBean.getVerifyCodeByName(getLoginUserName()); 
//get verifycodefrom database by name
Staff temp = staffBean.find(authcode);

if ( code.equals(authcode)){
    result ="success";
    temp.setVerifyCode("");
    temp.setVerifyStatus("Verified");
    staffBean.edit(temp);
}
return result;

我的门面:

@Override
 public String getVerifyCodeByName(String name) {
 Query q = em.createNamedQuery("Staff.getVerifyCodeByName");
 q.setParameter("name", name);
 return ((String) q.getSingleResult()).toString();

我的门面地点:

public String getVerifyCodeByName(String name);

我不确定哪个变量有错误。 :(

2 个答案:

答案 0 :(得分:1)

我猜问题就出现了:

Staff temp = staffBean.find(authcode);

您尝试根据字符串搜索Staff实例。但是你想要做的搜索应该基于一个Integer,因为这是你的@Id属性。

答案 1 :(得分:0)

Staff对象的主键是什么?你没有向我们展示全班。错误消息表示您需要传递一个Integer,并且您正在传递一个String。