7.hibernate的DML风格

public class UserManager
{
	public static void main(String[] args)throws Exception
	{
		UserManager mgr = new UserManager();
		mgr.updateUsers();
		HibernateUtil.sessionFactory.close();
	}
	private void updateUsers()throws Exception
	{
		//打开Session
		Session session = HibernateUtil.currentSession();
		//开始事务
		Transaction tx = session.beginTransaction();
		session.createQuery("update User set name=:newName")
				.setString("newName", "新名字xxx")
				.executeUpdate();

		//提交事务
		tx.commit();
		//关闭事务
		HibernateUtil.closeSession();
	}
}

你可能感兴趣的:(7.hibernate的DML风格)