使用Hibernate检索特定记录

时间:2012-10-16 06:53:31

标签: java hibernate

我想使用Hibernate从Data Base中检索特定记录。我想做的事情在下面的功能中进行了评论。

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;

}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

 public Customer getCustomer(Long customerIdFromCustomerListPage)
    throws Exception {

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

}