【问题标题】:criteria.list() shows null pointer exceptioncriteria.list() 显示空指针异常
【发布时间】:2013-02-23 18:14:01
【问题描述】:

我正在开发 Hibernate 3.3。最近我开始使用标准。我的注释类如下..

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

我的标准代码如下

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

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

【问题讨论】:

    标签: hibernate jakarta-ee


    【解决方案1】:

    将“已启用”的类型更改为“长”或根据您的要求为 db 架构中的列定义默认值。

    【讨论】:

    • 我已经知道我能做到。在这里,我发布了一个表注释类。但我还有很多。如果我更改注释类,我必须更改我的整个代码...
    • 然后在你的问题中提及你所知道的。其他人会知道你改变了/有很多没有遵循的课程写一些测试一些。('我还有更多')
    • 我更新了.. 经过大量搜索更改为 Long 或 BigDecimal 对象或其他对象是这样做的方法.. 感谢您的建议...
    【解决方案2】:

    尝试使用 NotFound 注释: 也许你的问题会得到解决

      @NotFound(action = NotFoundAction.IGNORE)
    

    【讨论】:

    • 我仍然收到此“空值已分配给原始类型设置器的属性”错误消息...
    • 将原语 long 改为 Long 类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多