criteria.list()显示空指针异常 [英] criteria.list() shows null pointer exception

查看:535
本文介绍了criteria.list()显示空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Hibernate 3.3。最近我开始使用标准。我的Annotation类如下。

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

private String username;
私人字符串密码;
私人字符串全名;
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;
}
}

我的标准代码如下

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

当isEnabled值为一行时,此代码显示错误。我知道它不能将空值转换为原始数据类型long。所以我想知道有没有其他方法可以跳过任何属性的空值。 (我发布了一个表类,并且我有更多的表具有相同种类的映射)

解决方案

更改'isenabled'到'长'或根据您的要求为db架构中的列定义默认值。


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)

解决方案

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

这篇关于criteria.list()显示空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆