A usage of hibernate

Source: Internet
Author: User
Tags throwable

Nonsense not much to say on the code
Import Java.util.list;import Java.util.map;import Org.hibernate.criterion.criterion;import Org.hibernate.criterion.order;import com. Homefurnishing.common.exceptions.myruntimeexception;import com. homefurnishing.common.entity.pagecond;/*** * @author Zhukovasky * @version 1.0 * @see This interface is used to make general additions and deletions for the hibernate frame, There may be some problems with doing a single table operation * */public interface Hibernatedao {/*** * save, Single table operation * */public void saveentity (String klass,object obj);p ubli c void updateentity (string klass,object obj);p ublic void insertentity (string klass,object obj);p ublic void Deleteentity ( String Klass,object obj);p ublic void Deleteentitybycascade (string klass,object[] obj);p ublic List Queryentitiesbycriteria (Criterion criteria, String klass,list<order> orders);p ublic List Queryentitiesbytemplate (String klass,object obj,criterion criteria,list<order> orders);p ublic object[] Queryentitiesbycriteria (Criterion criteria, String klass,pagecond page,list<order> orders);/*** * @author Zhukovasky * @see Template query * */publicObject[] Queryentitiesbytemplate (String klass,object obj,criterion criteria, Pagecond page,list<order> orders); Public <V> V expandentity (String klass,v obj)/**** * @author Zhukovasky * @param sqllocations Place SQL corresponding path name * @see i.e . String packagename= "Com.zhukovasky.common.utils";  * String Xmlname= "testxml.xml" * string sqlname= "Querymybudget"; * Then its value should be * "com.zhukovasky.common.utils.testxml.queryMyBudget" * program will automatically calculate the corresponding address and generate the actual path name * @param map<string,object > Parametermap Query Parameters * @return list<map> Returns the result as HashMap, for a single record * */public list<map> querynamesqlbyhibernate (String sqllocations,map<string,object> parametermap) throws myruntimeexception;/** * @author Zhukovasky * @param sqllocations Place SQL corresponding path name * @see i.e.string packagename= "Com.zhukovasky.common.utils";  * String Xmlname= "testxml.xml" * string sqlname= "Querymybudget"; * Then its value should be * "com.zhukovasky.common.utils.testxml.queryMyBudget" * program will automatically calculate the corresponding address and generate the actual path name * @param map<string,object > ParametermThe AP query parameter * @return list<map> returns the result as HashMap, for a single record * * */public list<map> querynamesqlbyhibernate (String Sqlloca Tions,map<string,object> Parametermap,pagecond page) throws myruntimeexception;/*** * @author Zhukovasky * @param Sqllocations Place Stored Procedure location * */public void Callprocedures (String sqllocations,map<string,object> parametermap);}

Import Java.io.file;import java.io.ioexception;import Java.io.serializable;import Java.util.list;import Java.util.map;import Javax.xml.parsers.parserconfigurationexception;import Org.hibernate.criteria;import Org.hibernate.entitymode;import Org.hibernate.sqlquery;import Org.hibernate.session;import Org.hibernate.criterion.criterion;import Org.hibernate.criterion.example;import Org.hibernate.criterion.Order; Import Org.hibernate.metadata.classmetadata;import Org.hibernate.transform.transformers;import Org.slf4j.Logger; Import Org.slf4j.loggerfactory;import org.xml.sax.saxexception;import com. Homefurnishing.common.databaseutil.basic.hibernatesessionfactory;import com. Homefurnishing.common.exceptions.myruntimeexception;import com. Homefurnishing.common.pagination.pagination;import com. Homefurnishing.common.xmlutils.xmlutils;import com. Homefurnishing.common.entity.pagecond;public class Hibernatedaoimpl implements Hibernatedao {private static final Logger log = Loggerfactory.getlogger (HibernatEdaoimpl.class);/*** * @param klass entity Name, class name * @param Obj Entity Object * @see This method may be problematic if the entity does not have sequence * */public void Savee Ntity (String Klass, object obj) {if (obj==null) {Log.error ("Saveentity Method object is empty!) ");} Session session=hibernatesessionfactory.getsessionfactory (). Opensession (); Log.info ("Save entity"); Session.save (Klass, obj); Session.flush ();} /*** * @param Klass entity Name, class name * @param Obj entity Object * */public void Updateentity (String Klass, Object Obj) {Session Session=hi Bernatesessionfactory.getsessionfactory (). Opensession (); Session.update (Klass, obj); Session.flush ();} /*** * @param Klass entity Name, class name * @param Obj entity Object * */public void InsertEntity1 (String Klass, Object Obj) {Session session=h Ibernatesessionfactory.getsessionfactory (). Opensession (); Session.save (Klass, obj); Session.flush ();} /*** * @param Klass entity Name, class name * @param Obj entity Object * */public void Deleteentity (String Klass, Object Obj) {//TODO Auto-gener Ated Method stub}/*** * @param klass entity Name, class name * @param Obj entity Object * */public void DeleteentitybycascadE (Class Klass, object[] obj) {//TODO auto-generated method stub}/*** * @param klass entity Name, class name * @param obj Entity Object * */publi C List Queryentitiesbycriteria (Criterion criteria, String klass,list<order> orders) {//TODO auto-generated method Stubreturn null;} /*** * @param Klass entity Name, class name * @param OBJ Entity Object * */@SuppressWarnings ("unchecked") public <V> V expandentity (String k Lass, V obj) {Session session=hibernatesessionfactory.getsessionfactory (). Opensession (); Classmetadata cl=hibernatesessionfactory.getsessionfactory (). Getclassmetadata (Klass); Serializable id=cl.getidentifier (obj, Entitymode.pojo); obj= (V) Session.get (Klass, id); return obj;} /** * @param Klass entity Name, class name * @param Obj entity object */public List queryentitiesbytemplate (String Klass, Object obj,criterion CR Iteria, list<order> orders) {Session session=hibernatesessionfactory.getsession (); Criteria Criterias=session.createcriteria (Klass). Add (Example.create (obj)). Add (criteria); List list=null;if (orders==null) {List=criterias.liSt ();} Else{for (Order iter:orders) {criterias.addorder (ITER);}} List=criterias.list (); return list;} /*** * @param Klass entity Name, class name * @param Obj entity Object * **/public void Insertentity (String Klass, Object Obj) {Session session=h Ibernatesessionfactory.getsessionfactory (). Opensession (); Session.save (Klass, obj); Session.flush ();}  /*** * @param Klass entity Name, class name * @param Obj entity Object * */public void Deleteentitybycascade (String Klass, object[] Obj) {//TODO Auto-generated Method stub}/*** * @param klass entity Name, class name * @param Obj Entity Object * */public object[] Queryentitiesbycriteria (Cr Iterion criteria, String klass,pagecond page, list<order> orders) {object[] Result=pagination.doquerypagecond ( Klass, criteria, orders, page); return result;} /*** * @param Klass entity Name, class name * @param Obj Entity Object * */public object[] queryentitiesbytemplate (String Klass, Object obj,crite Rion criteria, Pagecond page, list<order> orders) {object[] Result=pagination.doquerypagecond (Klass, criteria, Orders, page); return result;} /**** * @Author Zhukovasky * @param sqllocations Place SQL corresponding path name * @see i.e.string packagename= "Com.zhukovasky.common.utils";  * String Xmlname= "testxml.xml" * string sqlname= "Querymybudget"; * Then its value should be * "com.zhukovasky.common.utils.testxml.queryMyBudget" * program will automatically calculate the corresponding address and generate the actual path name * @param map<string,object > Parametermap Query Parameters * @return list<map> Returns the result as HashMap, for a single record * */public list<map> querynamesqlbyhibernate (String sqllocations,map<string, object> parametermap) throws Myruntimeexception{string systempath= System.getproperty ("User.dir"); String Realxmlpath=xmlutils.getrealsqlname (sqllocations); String Querysqlname=xmlutils.getrealsqlname (sqllocations); realxmlpath=systempath+file.separator+ "src" + File.separator+realxmlpath; String sql= ""; Boolean hasexceptions=false; Throwable Ee=null; Session Session=null; List<map> result=null;try {sql=xmlutils.loadsqlfromxml (Realxmlpath, Querysqlname, ParameterMap);} catch ( Parserconfigurationexception e) {log.error ("Compile XML Error"); ee=e;hasexceptIons=true;} catch (IOException e) {log.error ("file read-write exception"), Ee=e;hasexceptions=true;} catch (Saxexception e) {log.error ("Parse XML error"); ee= E;hasexceptions=true;} Finally{if (hasexceptions==true) {throw new myruntimeexception (EE);} Else{session=hibernatesessionfactory.getsession (); SQLQuery sqlquery=session.createsqlquery (SQL); String[] Realparameters=sqlquery.getnamedparameters (); for (String iter:realparameters) {if ( Parametermap.containskey (ITER)) {Sqlquery.setparameter (ITER, Parametermap.get (ITER));}} Sqlquery.setresulttransformer (Transformers.alias_to_entity_map); Result=sqlquery.list ();}} return result;} /**** * @author Zhukovasky * @param sqllocations Place SQL corresponding path name * @see i.e.string packagename= "Com.zhukovasky.common.utils";  * String Xmlname= "testxml.xml" * string sqlname= "Querymybudget"; * Then its value should be * "com.zhukovasky.common.utils.testxml.queryMyBudget" * program will automatically calculate the corresponding address and generate the actual path name * @param map<string,object > Parametermap Query Parameters * @return list<map> Returns the result as HashMap, for a single record * * */public list<map> QueryNamesqlbyhibernate (String sqllocations,map<string, object> parametermap, Pagecond page) throws Myruntimeexception{string systempath=system.getproperty ("User.dir"); String Realxmlpath=xmlutils.getrealsqlname (sqllocations); String Querysqlname=xmlutils.getrealsqlname (sqllocations); realxmlpath=systempath+file.separator+ "src" + File.separator+realxmlpath; String sql= ""; Boolean hasexceptions=false; Throwable Ee=null; Session Session=null; List<map> result=null;try {sql=xmlutils.loadsqlfromxml (Realxmlpath, Querysqlname, ParameterMap);} catch ( Parserconfigurationexception e) {log.error ("Compile XML Error"); ee=e;hasexceptions=true;} catch (IOException e) {log.error (" File read/write exception "); ee=e;hasexceptions=true;} catch (Saxexception e) {log.error ("Parse XML error"); ee=e;hasexceptions=true;} Finally{if (hasexceptions==true) {throw new myruntimeexception (EE);} Else{session=hibernatesessionfactory.getsession (); SQLQuery sqlquery=session.createsqlquery (SQL); String[] Realparameters=sqlquery.getnamedparameters (); for (String Iter:reAlparameters) {if (Parametermap.containskey (ITER)) {Sqlquery.setparameter (ITER, Parametermap.get (ITER));}} Sqlquery.setresulttransformer (Transformers.alias_to_entity_map); Result=pagination.doquerypagecond (SQLQuery, page);}} return result;} public void Callprocedures (string sqllocations,map<string, object> parametermap) {string systempath= System.getproperty ("User.dir"); String Realxmlpath=xmlutils.getrealsqlname (sqllocations); String Querysqlname=xmlutils.getrealsqlname (sqllocations); realxmlpath=systempath+file.separator+ "src" + File.separator+realxmlpath; String sql= ""; Boolean hasexceptions=false; Throwable Ee=null; Session session=null;try {sql=xmlutils.loadsqlfromxml (Realxmlpath, Querysqlname, Parametermap);} catch ( Parserconfigurationexception e) {log.error ("Compile XML Error"); ee=e;hasexceptions=true;} catch (IOException e) {log.error (" File read/write exception "); ee=e;hasexceptions=true;} catch (Saxexception e) {log.error ("Parse XML error"); ee=e;hasexceptions=true;} Finally{if (hasexceptions==true) {throw new Myruntimeexception (EE);} Else{session=hibernatesessionfactory.getsession (); SQLQuery sqlquery=session.createsqlquery (SQL); String[] Realparameters=sqlquery.getnamedparameters (); for (String iter:realparameters) {if ( Parametermap.containskey (ITER)) {Sqlquery.setparameter (ITER, Parametermap.get (ITER));}} Sqlquery.executeupdate ();}}}

A usage of hibernate

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.