hibernate 03

使用HQL查询所有记录

预见结果集

多个就用list处理 

单个就用 uniqueResult

        @Test
	public void fun1() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from com.lanou3g.User";
		Query query = session.createQuery(hql);
		List list = query.list();
		System.out.println(list);
		transaction.commit();
		session.close();
	}
        @Test
	public void fun2() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		String hql = "from com.lanou3g.User where id = 2";
		Query query = session.createQuery(hql);
		User user = query.uniqueResult();
		System.out.println(user);
		transaction.commit();
		session.close();
	}

使用问号占位符替代 

?从0开始

        @Test
	public void fun3() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		String hql = "from com.lanou3g.User where id = ?";
		Query query = session.createQuery(hql);
		query.setParameter(0, 2);
		User user = query.uniqueResult();
		System.out.println(user);
		transaction.commit();
		session.close();
	}

使用冒号占位符

冒号后面是占位符的名字

注意 冒号后面不要加空格

替换时 直接使用名字即可

        @Test
	public void fun4() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		String hql = "from com.lanou3g.User where username = :username and password = :password";
		Query query = session.createQuery(hql);
		query.setParameter("username", "wanglong");
		query.setParameter("password", "123");
		User user = query.uniqueResult();
		System.out.println(user);
		transaction.commit();
		session.close();
	}

分页

        @Test
	public void fun5() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		String hql = "from User";
		Query query = session.createQuery(hql, User.class);
		query.setFirstResult(0);
		query.setMaxResults(2);
		List list = query.list();
		System.out.println(list);
		transaction.commit();
		session.close();
	}

测试1对多




	
		
			
		
		
		
		
		
		
		
		
		
		
		 
		
			
			
			
	



	
		
			
		
		
		
		
		
		
		
		
		
		
		
		
	
public class Demo02 {
	@Test
	public void fun1() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		//创建一个客户两个联系人
		//表示客户和联系人的关系
		//将以上添加到数据库中
		Customer customer = new Customer();
		customer.setCust_name("蓝鸥科技");
		Linkman l1 = new Linkman();
		l1.setLkm_name("欣姐");
		Linkman l2 = new Linkman();
		l2.setLkm_name("韦姐");
		Set linkmans = customer.getLinkmans();
		linkmans.add(l1);
		linkmans.add(l2);
		l1.setCustomer(customer);
		l2.setCustomer(customer);
		session.save(customer);
		//session.save(l1);
		//session.save(l2);
		transaction.commit();
		session.close();
		
	}
	//为id为1的客户添加一个联系人
	@Test
	public void fun2() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		
		Customer customer = session.get(Customer.class, 1);
		Linkman l1 = new Linkman();
		l1.setLkm_name("hh");
		Set linkmans = customer.getLinkmans();
		linkmans.add(l1);
		l1.setCustomer(customer);
		session.save(l1);
		
		
		transaction.commit();
		session.close();
	}
	//删除id为1的客户的id为1的联系人
	@Test
	public void fun3() {
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		
		Customer customer = session.get(Customer.class, 1);
		Linkman linkman = session.get(Linkman.class, 1);
		
		Set linkmans = customer.getLinkmans();
		linkmans.remove(linkman);
		linkman.setCustomer(null);
		
		
		
		transaction.commit();
		session.close();
		
	}
}
测试多对多




	
		
			
		
		
		
		
		
		
		
		
			
			
		
		
	



	
		
			
		
		
		
		
		
		
			
			
		
		
	
        @Test
	public void fun1() {
		//创建两个员工
		//创建两个角色 保姆 保镖
		//这两员工 都是保姆也是保镖
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		User u1 = new User();
		u1.setUser_name("王龙");
		User u2 = new User();
		u2.setUser_name("王一鸣");
		Role r1 = new Role();
		r1.setRole_name("保姆");
		Role r2 = new Role();
		r2.setRole_name("保镖");
		Set roles1 = u1.getRoles();
		roles1.add(r1);
		roles1.add(r2);
		Set roles2 = u2.getRoles();
		roles2.add(r2);
		roles2.add(r2);
		r1.getUsers().add(u1);
		r1.getUsers().add(u2);
		r2.getUsers().add(u1);
		r2.getUsers().add(u1);
		session.save(u1);
		session.save(u2);
		session.save(r1);
		session.save(r2);
		
		transaction.commit();
		session.close();
	}
	//为王龙添加一个新角色
	@Test
	public void fun2() {
		
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		User user = session.get(User.class, 1);
		Role role = new Role();
		role.setRole_name("讲师");
		Set roles = user.getRoles();
		roles.add(role);
		role.getUsers().add(user);
		session.save(user);
		session.save(role);
		
		transaction.commit();
		session.close();
	}
	
	//为王龙删除一个id为3的角色
	@Test
	public void fun3() {
		
		Session session = HibernateUtil.getOpensession();
		Transaction transaction = session.beginTransaction();
		User user = session.get(User.class, 1);
		Role role = session.get(Role.class, 3);
		user.getRoles().remove(role);
		role.getUsers().remove(user);
		
		
		
		transaction.commit();
		session.close();
	}











你可能感兴趣的:(hibernate 03)