java - 使用 Hibernate 检索特定记录

标签 java 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;

}

最佳答案

尝试这样的事情

 public Customer getCustomer(Long customerIdFromCustomerListPage)
    throws Exception {

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

}

关于java - 使用 Hibernate 检索特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12909032/

相关文章:

java - 使用 hibernate 与 oracle 的驱动程序连接问题

java - 保存类别时如何设置父类别?

java - 链表以一种数据类型显示数据

java - 客户端 HTTP Post 到外部站点

java - 创建事件以删除表中的记录

hibernate - 从另一个 docker 容器连接到数据库

java.lang.IllegalStateException : org. hibernate .TransientPropertyValueException

java - 对于静态 ImageIcon,是否有 getClass().getResource() 的替代方法

java - 服务器浏览器如何在游戏工作中?

Spring Hibernate 事务管理