Hql查询
基本查询
@Test
//HQL语句查询所有对象 HQL经常用于多表查询
public void test(){
Session s = HibernateUtils.openSession();
Transaction tx = s.beginTransaction();
//String hql = "from cn.itheima.domain.Customer";
String hql = "from Customer";
//根据hql语句查询对象
Query query = s.createQuery(hql);
//返回list集合
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
s.close();
}
条件查询
@Test
//HQL语句条件查询对象,不可能出现任何数据库相关信息
public void test2(){
Session s = HibernateUtils.openSession();
Transaction tx = s.beginTransaction();
//String hql = "from cn.itheima.domain.Customer";
String hql = "from Customer where cust_id=1";
//根据hql语句查询对象
Query query = s.createQuery(hql);
//返回单个
Customer un = (Customer) query.uniqueResult();
System.out.println(un);
tx.comm