criteria.list() shows null pointer exception

余生长醉 提交于 2020-01-05 04:40:12

问题


I'am working on Hibernate 3.3. Recently I started using criteria. My Annotation class is as follows..

@Entity
@Table(name = "EMAG_ADMINLOGIN", schema = "EMAG_USER")
public class Adminlogin implements java.io.Serializable {

private String username;
private String password;
private String fullname;
private long isenabled;

@Id
@Column(name = "USERNAME", unique = true, nullable = false, length = 40)
public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}
@Column(name = "PASSWORD", length = 40)
public String getPassword() {
    return this.password;
}

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

@Column(name = "FULLNAME", nullable = false, length = 100)
public String getFullname() {
    return this.fullname;
}

public void setFullname(String fullname) {
    this.fullname = fullname;
}
@Column(name = "ISENABLED", precision = 0)
public long getIsenabled() {
    return this.isenabled;
}

public void setIsenabled(long isenabled) {
    this.isenabled = isenabled;
}
}

And my criteria code is as follows

Criteria criteria=session.createCriteria(Adminlogin.class);
criteria=criteria.add(Restrictions.eq("username", "admin"));
List list=criteria.list();

this code showing error when isEnabled value is null on a row. I know it can not convert null value to primitive data type long. So I want to know is there any other way to skip null value of any property . (I posted one table class, and i have more tables with same kind of class mappings)


回答1:


Change type of 'isenabled' to 'Long' or have default value defined for the column in db schema based on your requirements.




回答2:


Try to Use the NotFound annotation: may be your problem will be solved

  @NotFound(action = NotFoundAction.IGNORE)


来源:https://stackoverflow.com/questions/15043866/criteria-list-shows-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!