什么总是回报0

时间:2013-04-17 14:45:16

标签: java hibernate

我想用ID

的Hibernate检索数据

这是我的助手班:

public class ftHelper {

    Session session = null;

    public ftHelper(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List getFtResult(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;
}

    public FinancialTransactions getByID(){

        System.out.print("HERE");

       FinancialTransactions ft = null;

    try {

        org.hibernate.Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from FinancialTransactions where ID=" + 1);
        ft = new FinancialTransactions(1);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ft;

    }

我在这里使用getById函数来检索数据

我有一个控制器类

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author mert
 */
@ManagedBean
@RequestScoped
public class ftController {

    String beginDate;
    DataModel fts;
    ftHelper ft;
    int num;

    public int getNum() {
        return num;
    }



    /**
     * Creates a new instance of ftController
     */
    public ftController() {


    }

    public DataModel getFts() {

        if (fts == null) {
            fts = new ListDataModel(ft.getFtResult(beginDate));
        }
        return fts;
    }

    public int getID(){

        FinancialTransactions ftObject = ft.getByID();


        return num = ftObject.getAmount();

    }

}

在这个控制器中我使用getID函数来获取对象

在jsf中,我确实喜欢这样:

                      

但它总是打印0为什么?

我觉得我班上有问题,因为

我在我的控制器中编辑了getID()函数:

  public int getID(){
FinancialTransactions fta = new FinancialTransactions(5, "asdasdsa", 123);

        return num = fta.getAmount();
}

num也返回0为什么为什么?这有什么问题?

1 个答案:

答案 0 :(得分:0)

您没有在getByID()

中执行查询
public FinancialTransactions getByID(){

    System.out.print("HERE");

   FinancialTransactions ft = null;

try {

    org.hibernate.Transaction tx = session.beginTransaction();
    Query q = session.createQuery("from FinancialTransactions where ID=" + 1);

    // q.getResultList(); MISSING HERE
    ft = new FinancialTransactions(1);
} catch (Exception e) {
    e.printStackTrace();
}