-1

我想用带有 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 为什么为什么为什么?这里有什么问题?

4

1 回答 1

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();
}
于 2013-04-17T14:50:16.077 回答