uniqueresult
数据库中根据你的查询条件只会返回唯一结果,就可以用uniqueresult这个方法!否则就用list();其返回类型为Object
uniqueResult()方法通常是在业务方法查询语句中用到的,比如(Usersroles)getsession().createQuery("select ur from UsersRoles ur where ur.role.id = ? and ur.user.id = ?") .setparameter(0, roleId) .setParameter(1, userId) .uniqueResult();这时通表示只(从数据库中只返回一条记录)返回一个对象。当不确定时用List()方法
如果查询返回多个值用list()方法
- public void testQuery(){
- configuration config = new Configuration().configure();
- sessionfactory factory = config.buildSessionFactory(); //创建SessionFactory
- Session session = factory.openSession(); //创建Session
- session.begintransaction(); //开始事务
- Query query = session.createQuery("from Student");
- List list = query.list(); // 用list方法返回多个值
- //List list = session.createQuery("from Student").list();
- for(int i=0;i<list.size();i++){
- Student student = (Student)list.get(i);
- System.out.print(student.getId()+" ");
- System.out.print(student.getName()+" ");
- System.out.print(student.getSex()+" ");
- System.out.print(student.getBirthday());
- System.out.println();
- }
- session.getTransaction().commit(); //提交事务
- session.close(); //关闭Session
- }
[java] view plain copy print?
- public void testQuery(){
- Configuration config = new Configuration().configure();
- SessionFactory factory = config.buildSessionFactory(); //创建SessionFactory
- Session session = factory.openSession(); //创建Session
- session.beginTransaction(); //开始事务
- Query query = session.createQuery("from Student");
- List list = query.list(); // 用list方法返回多个值
- //List list = session.createQuery("from Student").list();
- for(int i=0;i<list.size();i++){
- Student student = (Student)list.get(i);
- System.out.print(student.getId()+" ");
- System.out.print(student.getName()+" ");
- System.out.print(student.getSex()+" ");
- System.out.print(student.getBirthday());
- System.out.println();
- }
- session.getTransaction().commit(); //提交事务
- session.close(); //关闭Session
- }
[java] view plaincopyprint?
- public void testGet(){
- Configuration config = new Configuration().configure();
- SessionFactory factory = config.buildSessionFactory();
- Session session = factory.openSession();
- session.beginTransaction();
- Query query = session.createQuery("from Student s where s.id=?");
- query.setString(0, "2");
- Student student = (Student)query.uniqueResult(); //当确定返回的实例只有一个或者null时 用uniqueResult()方法
- //Student student = (Student)session.createQuery("from Student s where s.id=?").setString(0,"5").uniqueResult();
- System.out.println(student.getName());
- System.out.println(student.getBirthday());
- session.getTransaction().commit();
- session.close();
- }
原文出处:http://blog.csdn.net/oh_mourinho/article/details/7788088
相关阅读
Spring整合HIbernate和springmvc 1.导入jar包 <properties> <org.springframework.version>4.2.0.RELEASE</org.springfram
HQL语句查询 // 使用HQL查询表的所有记录 Session session = HibernateUitl.getOpenSession()
7.1 Hibernate:内置生成器 -- assigned
assigned 是 Hibernate 内置的默认生成器策略,应用程序应该对持久化对象的唯一标识符赋值。 特点:可以跨数据库,人为控制主键生成,建
Hibernate状态 (merge,attachDirty,attachClean)
在Hibernate中的对象有三种状态,即:瞬时状态(Transient)、持久状态(Persistent)、脱管状态(Detached)1、瞬时状态(Transient)由new
Hibernate中many-to-one元素配置中的属性简介
通过many-to-one 元素,可以定义一种常见的与另一个持久化类的关联。这种关系模型是多对一关联(实际上是一个对象引用-译注):这个表的