Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177476
  • 博文数量: 39
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-28 19:16
文章分类

全部博文(39)

文章存档

2011年(34)

2010年(5)

我的朋友

分类: Java

2011-01-06 14:51:17

package com.bjsxt.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 * 实体对象查询
 * @author Administrator
 *
 */
public class SimpleObjectQueryTest1 extends TestCase {

 public void testQuery1() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //返回Student对象的集合
   //可以忽略select
   List students = session.createQuery("from Student").list();
   for (Iterator iter=students.iterator(); iter.hasNext();) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }
 
 public void testQuery2() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //返回Student对象的集合
   //可以忽略select,表可以使用别名
   List students = session.createQuery("from Student s").list();
   for (Iterator iter=students.iterator(); iter.hasNext();) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 } 

 public void testQuery3() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //返回Student对象的集合
   //可以忽略select,表可以使用as命名别名
   List students = session.createQuery("from Student as s").list();
   for (Iterator iter=students.iterator(); iter.hasNext();) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }
 
 public void testQuery4() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //返回Student对象的集合
   //使用select查询实体对象,必须采用别名
   List students = session.createQuery("select s from Student as s").list();
   for (Iterator iter=students.iterator(); iter.hasNext();) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }  
 
 
 public void testQuery5() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //不支持select * from .....这样的查询语句
   List students = session.createQuery("select * from Student").list();
   for (Iterator iter=students.iterator(); iter.hasNext();) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }  
 
}

阅读(792) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~