Hibernate查询语句

1:criteria查询

package com.itany.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class CriteriaQuery extends TestCase{

public void test(){
Session session=null;
try{
session=HibernateUtil.getsSession();
Criteria criteria=session.createCriteria(Student.class);
criteria.add(Expression.like("name", "%0%"));
List students=criteria.list();
for(Object o:students){
Student s=(Student) o;
System.out.println("姓名:"+s.getName());
System.out.println("班级:"+s.getClasses().getName());
}
}catch (Exception e) {
}finally{
HibernateUtil.close(session);
}
}
}

2:简单属性查询

package com.itany.hibernate;

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

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class SimplePropertyQuery extends TestCase{

//单一属性查询
public void testQuery() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery("select name from Student").list();
for(Iterator it=students.iterator();it.hasNext();){
String name=(String) it.next();
System.out.println("姓名:"+name);
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//多个属性查询
public void testQuery2() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery("select name,id from Student").list();
for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println("学号:"+obj[1]+",姓名:"+obj[0]);
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//多个属性查询(直接返回实体对象)
public void testQuery3() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery("select new Student(id,name) from Student").list();
for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+", 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

3:简单对象查询

package com.itany.hibernate;

import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
import com.itany.util.HibernateUtil;

public class SimpleObjectQuery extends TestCase{

//在hibernate中
public void testQuery() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

//List students=session.createQuery("from Student").list();
//在HQL中没有select * 要想使用可以使用别名代替
List students=session.createQuery("select s from Student s").list();
for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//使用iterate遍历的N+1的问题
//1:首先查询所有对象的id
//N:首先根据id在缓存中查询,如果不存在匹配的数据,再在数据库中根据id查询
public void testQuery2() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

Iterator it=session.createQuery("from Student").iterate();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//list与Iterator的区别
//list每次调用都发出sql语句,并将结果写入缓存中,但它不会读取缓存数据
//Iterator开始会出现N+1的问题,但是Iterator会利用缓存数据,当缓存中有数据的时候,它的效率会比list高
public void testQuery3() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

//List students=session.createQuery("from Student").list();
//在HQL中没有select * 要想使用可以使用别名代替
List students=session.createQuery("select s from Student s").list();
for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
System.out.println("Iterator+***********");
Iterator it=session.createQuery("from Student").iterate();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
public void testQuery4() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

System.out.println("Iterator1+***********");
Iterator it=session.createQuery("from Student").iterate();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
System.out.println("Iterator1+***********");
Iterator it1=session.createQuery("from Student").iterate();
while(it1.hasNext()){
Student s=(Student) it1.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

4:简单条件查询

package com.itany.hibernate;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class SimpleConditionQuery extends TestCase{

//不建议使用,容易被注入
public void testQuery1() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery("select new Student(s.id,s.name) " +
"from Student s where s.name like ‘%1%‘").list();
for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}

public void testQuery2() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

/*Query query=session.createQuery("select new Student(s.id,s.name) " +
"from Student s where s.name like ?");
query.setParameter(0, "%1%");
List students=query.list();*/
//链式调用
List students=session.createQuery
("select new Student(s.id,s.name) " +
"from Student s where s.name like ?")
.setParameter(0, "%1%").list();
for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//推荐使用参数名来传递参数
public void testQuery3() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

Query query=session.createQuery
("select new Student(s.id,s.name) " +
"from Student s where s.name like :name");
query.setParameter("name", "%1%");
List students=query.list();

for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//多条件查询
public void testQuery4() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

Query query=session.createQuery
("select new Student(s.id,s.name) " +
"from Student s where s.name like :name " +
"and s.id=:id");
query.setParameter("name", "%1%");
query.setParameter("id", 2);
List students=query.list();

for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//in条件
public void testQuery5() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

Query query=session.createQuery
("select new Student(s.id,s.name) " +
"from Student s where s.id in(:id)");
query.setParameterList("id",new Object[]{1,2,3});
List students=query.list();

for(Iterator it=students.iterator();it.hasNext();){
Student s=(Student) it.next();
System.out.println("学号:"+s.getId()+" , 姓名:"+s.getName());
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//date_format()(mysql的专用函数)
//查询某个时间的数据
public void testQuery6() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

Query query=session.createQuery
("select s.id,s.name,s.createTime " +
"from Student s " +
"where date_format(s.createTime,‘%Y-%m‘)=:createTime");
query.setParameter("createTime","2015-03");
List students=query.list();

for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println("学号:"+obj[0]+",姓名:"+obj[1]+","+obj[2]);
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
public void testQuery7() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Query query=session.createQuery
("select s.id,s.name,s.createTime " +
"from Student s " +
"where s.createTime between :createTime1 " +
"and :createTime2");
query.setParameter("createTime1",sdf.parse("2015-01-10 00:00:00"));
query.setParameter("createTime2",sdf.parse("2015-03-23 23:59:59"));
List students=query.list();

for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println("学号:"+obj[0]+",姓名:"+obj[1]+","+obj[2]);
}
//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

5:sql原生查询语句

package com.itany.hibernate;

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

import org.hibernate.Session;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class SQLQuery extends TestCase {

public void testQuery1() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createSQLQuery("select * from t_student").list();
for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println("学号:"+obj[0]+",姓名:"+obj[1]+","+obj[2]);
}

//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

6:分页查询

package com.itany.hibernate;

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

import org.hibernate.Session;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class PageQuery extends TestCase{

public void testQuery1() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery("from Student")
.setFirstResult(1)
.setMaxResults(3)
.list();
System.out.println(students.size());

//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

7:对象导航查询

package com.itany.hibernate;

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

import org.hibernate.Session;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class ObjectNavQuery extends TestCase{

public void testQuery1() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery
("select s.name " +
"from Student s " +
"where s.classes.name like ‘%1%‘").list();
System.out.println(students.size());
for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println("学号:"+obj[0]);
}

//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

8:连接查询

package com.itany.hibernate;

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

import org.hibernate.Session;

import com.itany.util.HibernateUtil;

import junit.framework.TestCase;

public class JoinQuery extends TestCase{
//内连接:关联条件用表的外键自动关联
public void testQuery1() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery
("select s.name,c.name " +
"from Student s " +
"inner join s.classes c").list();
System.out.println(students.size());
for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println(""+obj[0]+" , "+obj[1]);
}

//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
//左外连接
public void testQuery2() {
Session session=null;
try{
//通过session工厂创建session
session=HibernateUtil.getsSession();
//开启事物
session.beginTransaction();

List students=session.createQuery
("select s.name,c.name " +
"from Student s " +
"left join s.classes c").list();
System.out.println(students.size());
for(Iterator it=students.iterator();it.hasNext();){
Object[] obj=(Object[]) it.next();
System.out.println(""+obj[0]+" , "+obj[1]);
}

//提交事物
session.getTransaction().commit();
}catch (Exception e) {
//回滚事物
session.getTransaction().rollback();
}finally{
HibernateUtil.close(session);
}
}
}

时间: 2024-10-25 00:44:53

Hibernate查询语句的相关文章

SSH框架整合遇到的错误——Hibernate查询语句出现错误

在调试前台注册界面,填写注册信息,用户名Ajax异步验证时报错,报错文件在Dao文件的查询语句中. 报错信息: java.lang.IllegalArgumentException: org.hibernate.QueryException: Legacy-style query parameters (`?`) are no longer supported; use JPA-style ordinal parameters (e.g., `?1`) instead : from com.it

hibernate查询语句hql中的占位符?参数与命名参数:name设值方式搞混

先贴出异常 Struts has detected an unhandled exception: Messages: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1 File: org/hibernate/engine/query/spi/ParameterMetadata.java Stacktraces org.h

Hibernate 插入,修改,删除,查询语句

/* *具体操作hibernate的类 *增加,删除,修改,按ID查询,模糊查询,查询全部 **/ public class PersonOperate { //在hibernate中所有操作都是由Session完成 public Session session = null; //在构造方法中实例化Session对象 public PersonOperate(){ //找出hibernate的配置 Configuration config = new Configuration().confi

Hibernate 关于执行sql查询语句(转)

原文  http://www.yshjava.cn/post/543.html 主题 SQLHibernate Hibernate对原生SQL查询的支持和控制是通过SQLQuery接口实现的.通过Session接口,我们能够很方便的创建一个SQLQuery(SQLQuery是一个接口,在Hibernate4.2.2之前,默认返回的是SQLQuery的实现类--SQLQueryImpl对象,在下文中出现的SQLQuery如非注明,都是指该子类)对象来进行原生SQL查询: session.creat

[原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

Hibernate HQL查询语句总结

Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";List list=session.CreateQuery(hql).list();上面的代码执行结果是,查询出User实体对象所对应的所有数据,而且将数据封装成User实体对象,并且放入List中返回.这里需要注意的是,Hibernate的实体查询存在着对继承关系的判定,比如我们前面讨论映射实体继承关系中的E

hibernate的hql查询语句

hibernate的hql查询语句 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,这里仅仅贴出这三个bean的属性代码: Special类: public class Special { private int id; private String name; private String type; private Set<Classroom> rooms; .

hibernate的hql查询语句总结

这篇随笔将会记录hql的常用的查询语句,为日后查看提供便利. 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,这里仅仅贴出这三个bean的属性代码: Special类: public class Special { private int id; private String name; private String type; private Set<Classro

Hibernate学习之hql查询语句

*  页面上数据的字段和数据库中字段差不多,这个时候,采用迫切连接  结构比较好,如果页面上的字段很少,要按照需求加载数据,采用带构造函数的select查询 实例讲解:转自:http://www.cnblogs.com/xiaoluo501395377/p/3376256.html 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,这里仅仅贴出这三个bean的属性代码: