hibernate中session,HQL,持久化,一对多,多对一
package com.hanqi.test;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hanqi.entity.Course;
import com.hanqi.entity.Teacher;
import com.hanqi.entity.User;
public class Test01 {
private SessionFactory sf=null;
private Session se=null;
private Transaction ts;
//在测试用例执行之前自动执行的方法
//一般用来初始化的公用对象
//前置方法
@Before
public void init()
{
//1获取配置文件
Configuration cfg=new Configuration().configure();
//2注册配置
ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
//3获取SessionFactory(相当于JDBC)
sf =cfg.buildSessionFactory(sr);
System.out.println(sf);
//4产生Session
se=sf.openSession();
//5启动事务
ts=se.beginTransaction();
}
//后置方法
//一般用来释放资源
@After
public void destroy()
{
//7提交事务
ts.commit();
//8释放资源
se.close();
sf.close();
}
//测试Hibernate连接数据库
@Test
public void test()
{
//1获取配置文件
Configuration cfg=new Configuration().configure();
//2注册配置
ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
//3获取SessionFactory(相当于JDBC)
SessionFactory sf =cfg.buildSessionFactory(sr);
System.out.println(sf);
//4产生Session
Session se=sf.openSession();
//5启动事务
ts=se.beginTransaction();
//6操作事务
//添加数据
//实例化的新对象处于临时状态
User u1=new User();
u1.setBirthday(new Date());
u1.setMoney(2000);
u1.setPassword("123456");
u1.setUserName("测试一");
//保存数据
//通过save方法把对象从临时状态转成持久化状态
se.save(u1);
System.out.println(u1);
}
//测试查询方法
@Test
public void test1()
{
//查询数据
//提供两个参数:
//1需要返回哪一个持久化类的实例
//2实例的标识(数据的主键值)
//通过Session的get方法获得的对象处于持久化状态
User u2=(User)se.get(User.class,3);
u2.setUserName("修改的");
System.out.println(u2);
//删除
se.delete(u2);//使持久化对象进入删除状态
}
@Test
public void test2()
{
User u1=(User)se.get(User.class, 4);
System.out.println(u1);
User u2=(User)se.get(User.class, 4);//立即加载数据
System.out.println(u2);
}
@Test
public void test3()
{
User u1=(User)se.load(User.class, 4);//延时加载 懒加载
System.out.println("dsad");
System.out.println(u1);//当使用时才加载
}
//测试游离状态
@Test
public void test4()
{
//得到持久化状态的对象
User u1=(User)se.get(User.class,4);
System.out.println("u1="+u1);
//关闭session
se.close();
//使u1进入游离状态
u1.setUserName("测试游离");
//把OID置成null
u1.setUserID(null);
//重新创建session
se=sf.openSession();
//重新开启事务
ts=se.beginTransaction();
se.saveOrUpdate(u1);
System.out.println(u1);
}
//测试HQL
@Test
public void test5()
{
//使用HQL
//1创建Query对象
Query qu=se.createQuery("from User where userID >? and "
+ "userName like '测试%' order by userID");
//设置占位符
qu.setInteger(0, 10);
//执行查询
List<User> lu=qu.list();
//方法链调用
//lu=se.createQuery("from User where userID >?").setInteger(0, 10).list();
for(User e:lu)
{
System.out.println(e);
}
}
//测试分页
@Test
public void test6()
{
//设置开始行号:页码=2
//(页码-1)*每页行数
List<User> lu=se.createQuery("from User order by userID")
.setMaxResults(5)
.setFirstResult(5)
.list();
for(User e:lu)
{
System.out.println(e);
}
}
//分组查询
@Test
public void test7()
{
List<Object>lo=se.createQuery("select userName from User group by userName").list();
for(Object o:lo)
{
System.out.println(o);
}
}
//测试投影查询
@Test
public void test8()
{
List<Object[]>lo=se.createQuery("select userID,userName from User").list();
for(Object[] o:lo)
{
System.out.println(o[0]+" "+o[1]);
}
System.out.println("dasdasadfa");
//返回持久化对象的集合
List<User> lu=se.createQuery("select new User(userID,userName) from User").list();
for(User u:lu)
{
System.out.println(u);
}
}
//测试多对一
@Test
public void test10()
{
Course cs=(Course)se.get(Course.class,"9-888");
System.out.println(cs);
}
//测试一对多
@Test
public void test11()
{
Teacher tc=(Teacher)se.get(Teacher.class,"831");
System.out.println(tc);
}
}