hibernate的hql帮助类

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.omg.Dynamic.Parameter;


public class HibernateHelper {
static SessionFactory sf=null;
static{
Configuration cf=new Configuration().configure();
sf=cf.buildSessionFactory();
}
//1、得到session。
public static Session getcurrentsession(){
Session session=sf.getCurrentSession();
return session;
}
public static Session opensession(){
Session session=sf.openSession();
return session;
}
//2、load提供一个返回一个对象的方法
public static Object findbyid(Class clazz, Serializable id){
Session s=HibernateHelper.getcurrentsession();
Transaction ts=s.beginTransaction();
Object obj=null;
try {
obj=s.load(clazz, id);
//提交事务
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (ts!=null) {
ts.rollback();
}
e.printStackTrace();
}finally{
if (s!=null&&s.isOpen()) {
s.close();
}
return obj;
}
}
//3、统一的一个修改和删除(批量 hql) hql"delete upate ...??"
public static void executeUpdate(String hql,String [] parameters){
Session s=null;
Transaction tx=null;
try {
s=opensession();
tx=s.beginTransaction();
Query query=s.createQuery(hql);
//先判断是否有参数要绑定
if(parameters!=null&& parameters.length>0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
query.executeUpdate();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null&&s.isOpen()){
s.close();
}
}
}
//4、统一的添加的方法
public static void save(Object obj){
Session s=null;
Transaction tx=null;
try {
s=opensession();
tx=s.beginTransaction();
s.save(obj);
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null && s.isOpen()){
s.close();
}
}
}
//5、只返回一个数据
public static String executeQuery_onepara(String hql,String parameter[]){
String s=null;
//创建session
Session session=HibernateHelper.getcurrentsession();
//创建事务
Transaction ts=session.beginTransaction();
try {
Query q=session.createQuery(hql);
if (parameter!=null&&parameter.length>0) {
for (int i = 0; i < parameter.length; i++) {
q.setString(i, parameter[i]);
}
}
s=(String)q.uniqueResult();
//提交事务
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (ts!=null) {
ts.rollback();
}
e.printStackTrace();
}finally{
if (session!=null&&session.isOpen()) {
session.close();
}
}
return s;
}
//6、只返回一个对象
public static Object executeQueryForObject(String hql,String parameter[]){
Object o=null;
//创建session
Session session=HibernateHelper.getcurrentsession();
//创建事务
Transaction ts=session.beginTransaction();
try {
Query q=session.createQuery(hql);
if (parameter!=null&&parameter.length>0) {
for (int i = 0; i < parameter.length; i++) {
q.setString(i, parameter[i]);
}
}
o=q.uniqueResult();
//提交事务
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (ts!=null) {
ts.rollback();
}
e.printStackTrace();
}finally{
if (session!=null&&session.isOpen()) {
session.close();
}
}
return o;
}
//7、分页

//提供提议分页
//通过参数绑定进行查询,
public static List executeQuery_bypage(String hql,String parameter[],int pagesize,int pagenow){
List list=null;
//创建session
Session session=HibernateHelper.getcurrentsession();
//创建事务
Transaction ts=session.beginTransaction();
try {
Query q=session.createQuery(hql);
if (parameter!=null&&parameter.length>0) {
for (int i = 0; i < parameter.length; i++) {
q.setString(i, parameter[i]);
}
}
q.setFirstResult((pagenow-1)*pagesize).setMaxResults(pagesize);
list=q.list();
//提交事务
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (ts!=null) {
ts.rollback();
}
e.printStackTrace();
}finally{
if (session!=null&&session.isOpen()) {
session.close();
}
}
return list;
}
//通过参数绑定进行查询,
public static List executeQuery_common_currentSe_setParameter(String hql,String parameter[]){
List list=null;
//创建session
Session session=HibernateHelper.getcurrentsession();
//创建事务
Transaction ts=session.beginTransaction();
try {
Query q=session.createQuery(hql);
if (parameter!=null&&parameter.length>0) {
for (int i = 0; i < parameter.length; i++) {
q.setString(i, parameter[i]);
}
}
list=q.list();
//提交事务
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (ts!=null) {
ts.rollback();
}
e.printStackTrace();
}finally{
if (session!=null&&session.isOpen()) {
session.close();
}
}
return list;
}
}

相关推荐