2012-10-16 80 views
1

我想使用Hibernate從數據庫檢索特定記錄。我想要做的是在下面的函數中註釋。使用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; 

} 
+1

,問題是......? – white

+0

如何閱讀一些Hibernate文檔?這是非常基本的東西。順便說一句,如果你對一個客戶感興趣,返回一個列表並沒有多大意義。請參閱http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Session.html#get%28java.lang.Class,%20java.io.Serializable%29和http:// docs。 jboss.org/hibernate/orm/3.6/reference/en-US/html_single/#objectstate-loading –

+0

是的你是對的,我剛剛舉了一個示例演示了我想做什麼, – Hasan

回答

1

嘗試這樣的事情

public Customer getCustomer(Long customerIdFromCustomerListPage) 
    throws Exception { 

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

} 
+0

如何迭代對象(Customer)使用JSTL從Jsp上面的代碼獲取?它給出了一個錯誤說:「不知道如何迭代提供的」項目「< for each >」.. – Hasan

+0

它是對象,你不能迭代它,你只需要使用它,而不會迭代 –

+0

哦,謝謝你RK。 – Hasan

相關問題