Hibernate packaged Tool Classes

Source: Internet
Author: User
Tags commit int size object object rollback
Package util;
Import java.util.ArrayList;

Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;

Import Org.hibernate.service.ServiceRegistryBuilder; Import entity.

UserInfo;
  
 public class Hibernateutil {private static sessionfactory sessionfactory; /** * @return Get the Session factory */public static sessionfactory Getsessionfactory () {//First step: Read Hibernate's configuration file hibernamte.cfg.x
	ML file configuration con=new configuration (). Configure (); Step Two: Create the Service Registration Builder object, load all configuration information through the configuration object Serviceregistrybuilder regbulider=new serviceregistrybuilder (). Applysettings (
	Con.getproperties ());
	Create a registration service Serviceregistry reg=regbulider.buildserviceregistry ();
	Step three: Create a session factory Sessionfactory Sessionfactory=con.buildsessionfactory (REG);
  return sessionfactory; }/** * @return Gets the Session object */public static session getsession () {REturn getsessionfactory (). Opensession ();
	}/** * @param obj Add data * @return */public static Boolean add (Object obj) {Session session=null;
	Transaction Tran=null;
	Boolean result=false;
		try {session=getsession ();
		Tran=session.begintransaction ();
		Session.save (obj);
		Tran.commit ();
	Result=true;
	   } catch (Exception e) {if (tran!=null) {//Things Roll back tran.rollback ();
		}} finally {if (session!=null) {///close session session.close ();
  }} return result; 
		}/** * @return Update data * parameter for modified primary key ID Object */public static Boolean update (Object object) {Session session=null;
		Transaction Tran=null;
		Boolean result=false;
			try {session=getsession ();
			Tran=session.begintransaction ();
			Session.update (object);
			Tran.commit ();
		Result=true;
		   } catch (Exception e) {if (tran!=null) {//Things Roll back tran.rollback ();
		}} finally {if (session!=null) {///close session session.close ();	}} return result;
	    }/** * @param c * @param obj queries a data based on the primary Key's ID number * @return */public static Object get (Class c,int obj) {
		Session Session=null;
		Object Object=null;
			try {session=getsession ();
		Object=session.get (C,obj);
			} catch (Exception e) {} finally {if (session!=null) {///close session session.close ();
  }} return object;
		}/** * @param obj * @return Delete data */public static Boolean delete (Object obj) {Session session=null;
		Transaction Tran=null;
		Boolean result=false;
			try {session=getsession ();
			Tran=session.begintransaction ();
			Session.delete (obj);
			Tran.commit ();
		Result=true;
		   } catch (Exception e) {if (tran!=null) {//Things Roll back tran.rollback ();
			}} finally {if (session!=null) {///close session session.close ();
  }} return result; }/** * @param <T> query multiple records * @param SQL SQL statement * @param param parameter array * @return */@SupPresswarnings ("unchecked") public static <T> list<t> query (String sql,string[] param) {list<t> lis
	  T=new arraylist<t> ();
	   Session Session=null;
			try {session=getsession ();
			Query query=session.createquery (SQL);	
				if (Param!=null) {for (int i=0;i<param.length;i++) {query.setstring (i,param[i]);
		}} list=query.list ();
			} catch (Exception e) {} finally {if (session!=null) {session.close ();
  }} return list;
	  }/** * @param sql * @param param query A single record * @return */public static Object Queryone (String sql,string[] param) {
	  Object Object=null;
	   Session Session=null;
			try {session=getsession ();
			Query query=session.createquery (SQL);	
				if (Param!=null) {for (int i=0;i<param.length;i++) {query.setstring (0,param[i]);
			} object=query.uniqueresult (); }} catch (Exception e) {} finally {if (session!=null) {session.close ();
  }} return object; }/** * @param <T> * @param SQL * @param param * @param page * @param size * @return Realize paged query */@SuppressWarni NGS ("unchecked") public static <T> list<t> querybypage (String sql,string[] param,int page,int size) {Lis
	  T<t> list=new arraylist<t> ();
	   Session Session=null;
			try {session=getsession ();
			Query query=session.createquery (SQL);	
				if (Param!=null) {for (int i=0;i<param.length;i++) {query.setstring (i,param[i]);
			}}//filter bar number Query.setfirstresult ((page-1) *size);
			Query.setmaxresults (size);
		List=query.list ();
			} catch (Exception e) {} finally {if (session!=null) {session.close ();
  }} return list; }/** * @param hql * @param pras * @return The number of returned data */public static int GetCount (String hql, string[] pras) {int ResU
	= 0;
	Session s = null;
		try {s = getsession ();
		Query q = s.createquery (HQL); if (Pras! = null) {for (int i= 0; i < pras.length;
			i++) {q.setstring (I, pras[i]);
	}} resu = Integer.valueof (Q.iterate (). Next (). ToString ());
	} catch (Exception e) {e.printstacktrace ();
	} finally {if (s! = null) s.close ();
} return resu; }
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.