hibernate createquery

	@SuppressWarnings("unchecked")
	public static List<Person> listAll()
	{
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction(); //开启事务
		
		List<Person> list = null;
		
		try
		{
			Query query = session.createQuery("from Person"); //hql语句,Hibernate查询语句
			
			list = (List<Person>)query.list();
			
			tx.commit();
		}
		catch(Exception ex)
		{
			System.out.println("增加用户异常发生!");
			if(null != tx)
			{
				tx.rollback();
			}
		}
		finally
		{
			HibernateUtil.closeSession(session);
		}
		
		return list;
	}

你可能感兴趣的:(Hibernate,session)