1

我想使用 Hibernate 从数据库中检索特定记录。我想要做的在函数下面注释。

public List<Customer> showCustomer(long customerIdFromCustomerListPage)
        throws Exception {

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    List<Customer> customerList = new ArrayList<Customer>();
    try {
        transaction = session.beginTransaction();

        **//Select * from Customers where customerId="customerIdFromCustomerListPage"**

        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return customerList;

}
4

1 回答 1

1

尝试这样的事情

 public Customer getCustomer(Long customerIdFromCustomerListPage)
    throws Exception {

      Session session = HibernateUtil.getSessionFactory().openSession();
      Customer customer = (Customer )session.get(Customer.class, customerIdFromCustomerListPage); 
      return customer ;

}
于 2012-10-16T07:25:05.790 回答