Hibernate基本使用

	public void testSave() {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();

			User1 user = new User1();
			user.setName("张三");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());			
			session.save(user);

			tx.commit();//提交事务
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();//异常回滚
		}finally {
			HibernateUtils.closeSession(session);
		}
	}

你可能感兴趣的:(Hibernate)