Java 为什么返回空值?

Java 为什么返回空值?,java,hibernate,Java,Hibernate,我已成功发送日期值。我从“作用域变量”中进行了检查 这是my bean中调用helper中函数的函数: public DataModel getFt() { ftDataModel = new ListDataModel((List) fthelper.getByBeginDate(beginDate)); return ftDataModel; } 这是将beginDate发送到Hibernate的函数。但在这里它返回null。为什么? public FinancialTra

我已成功发送日期值。我从“作用域变量”中进行了检查

这是my bean中调用helper中函数的函数:

public DataModel getFt() {
    ftDataModel = new ListDataModel((List) fthelper.getByBeginDate(beginDate));
    return ftDataModel;
}
这是将
beginDate
发送到Hibernate的函数。但在这里它返回null。为什么?

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);
}
公共金融交易getByBeginDate(字符串beginDate){
列表FtList=null;
试一试{
org.hibernate.Transaction tx=session.beginTransaction();
Query q=session.createQuery(“来自FinancialTransactions,其中日期=”“+beginDate+”);
FtList=(List)q.List();
}捕获(例外e){
e、 printStackTrace();
}
返回FtList.get(0);
}
其中一个:

org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from FinancialTransactions where DATE='" + beginDate + "'");

抛出异常。使用调试器或简单系统输出。。。要查找这两个参数中的哪一个。

您应该在查询中使用参数:

Query q = session.createQuery("from FinancialTransactions where DATE=:date").setParameter("date",beginDate);
不应存在内部异常,否则FtList.get(0)也将导致异常


我的猜测正在形成。您可以使用日期的字符串表示。因为在转换过程中可能会出现很多错误,所以这总是一个坏主意。

因为查询没有找到任何东西?可能查询没有返回任何数据,请尝试单独运行查询,并检查它返回了多少行。是否打印了异常堆栈跟踪?旁注:您确实应该清理输入。这是可以注射的@Brian是的,这个
where DATE='“+beginDate+”“
非常美味:-如果是这样的话,异常堆栈跟踪就会被打印出来。我使用“.”作为分隔符。这是错误的吗?我也不知道“FtList.get(0)”是什么意思?这是错误的吗?返回FtList.get(0);如果查询失败,会抛出异常,因为列表错误(FtList)将为null。使用“.”作为分隔符没有问题,但您的数据库可能会使用另一种格式。db中的日期字段是VARCHAR,因此我认为没有问题。FtList.get(0);这是什么意思?返回FtList.get(0)是方法getByBeginDate的最后一条语句。您应该打开hibernate查询日志记录,检查为查询生成的语句是否符合预期并返回正确的结果。可能beginDate有小时/分钟/秒,而db字段没有。