为什么这会返回null? [英] Why does this return null?

查看:158
本文介绍了为什么这会返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功发送了日期值。我检查了'Scoped Variables'。



这是我的bean中的一个函数,它在helper中调用函数:

  public DataModel getFt(){
ftDataModel = new ListDataModel((List)fthelper.getByBeginDate(beginDate));
返回ftDataModel;

$ / code>

这是发送 beginDate 到Hibernate。但是在这里它返回null。为什么?

  public FinancialTransactions getByBeginDate(String beginDate){
List< FinancialTransactions> FtList = null;
尝试{
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery(from FinancialTransactions where DATE ='+ beginDate +');
FtList =(List< FinancialTransactions>)q.list();

} catch(Exception e){
e.printStackTrace();
}
return FtList.get(0);
}


解决方案

query:

  Query q = session.createQuery(from FinancialTransactions where DATE =:date)。setParameter(date, BEGINDATE); 

应该没有内部异常,否则FtList.get(0)也会导致异常。 / p>

我的猜测是格式化。您使用日期的字符串表示形式。总是一个坏主意,因为有许多事情可能在转换中出错。


I sent the date value successfully. I checked from the 'Scoped Variables'.

This is a function in my bean that calls a function in helper:

public DataModel getFt() {
    ftDataModel = new ListDataModel((List) fthelper.getByBeginDate(beginDate));
    return ftDataModel;
}

This is the function that sent beginDate to Hibernate. But here it returns null. Why?

public FinancialTransactions getByBeginDate(String beginDate){            
    List<FinancialTransactions> FtList = null;        
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from FinancialTransactions where DATE='" + beginDate + "'");
        FtList = (List<FinancialTransactions>) q.list();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return FtList.get(0);
}

解决方案

You should use parameters in your query:

Query q = session.createQuery("from FinancialTransactions where DATE=:date").setParameter("date",beginDate);

There should be no inner exception or else FtList.get(0) would result in exception also.

My guess is formating. You use string representation of dates. Always a bad idea since there are many things that can go wrong in convertions.

这篇关于为什么这会返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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