Hibernate Frame Road (d) Hibernate Query method

Source: Internet
Author: User
Tags set set

OID Query

    /*** OID Query * query based on ID * one-to-many queries*/@Test Public voidTest () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction ();//Query the customer 's own contact person, first query customer id=1 customer, according to customer inquiries contactUser user = Session.get (user.class, 1); //get a set set of contactsSet<link> Link =User.getlink ();//number of output contactsSystem.out.println (Link.size ());        Ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL Query All

    /*** Check all *HQL*/@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction ();//query all customer from Customer entity class nameQuery query = Session.createquery ("From User"); List<User> list =query.list ();  for(User user:list) {System.out.println (User.getuid ()+ "\ T" +user.getuname ()); } System.out.println ("-------------------------"); Query Query2= Session.createquery ("From Link"); List<Link> List2 =query2.list ();  for(Link link:list2) {System.out.println (Link.getcid ()+ "\ T" +link.getcname ());        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL condition Query

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query object from Cuastomer where user_id=? and user_name=?Query query = Session.createquery ("from User C where c.uid=?") and c.uname=? "); /*** Fill the placeholder with data; placeholder Poute starting from 0 * The first parameter is subscript * The second parameter is a value*/Query.setparameter (0, 1); Query.setparameter (1, "Zhang San"); List<User> list =query.list ();  for(User user:list) {System.out.println (User.getutel ());        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL Fuzzy Query

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query objectQuery query = Session.createquery ("from User C where c.uname like?")); /*** Fill the placeholder with data; placeholder Poute starting from 0 * The first parameter is subscript * The second parameter is a value*/Query.setparameter (0, "% sheet%"); List<User> list =query.list ();  for(User user:list) {System.out.println (User.getutel ());        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL sort

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query Object DESC reverse ASC Positive OrderQuery query = Session.createquery ("From the User Order by uId ASC"); List<User> list =query.list ();  for(User user:list) {System.out.println (User.getuid ());        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL Sub-page

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query objectQuery query = Session.createquery ("From User"); Query.setfetchsize (0);//Start PositionQuery.setmaxresults (3);//number of displays per pagelist<user> list =query.list ();  for(User user:list) {System.out.println (User.getuid ());        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

HQL Querying a field

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query objectQuery query = Session.createquery ("Select UId from User"); List<Object> list =query.list ();  for(Object object:list) {System.out.println (object);        } ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

hql function Query

@Test Public voidtest2 () {Sessionfactory factory=NULL; Session Session=NULL; Transaction TS=NULL; Try{Factory=hibernateutils.getsessionfactory (); Session=factory.opensession (); TS=session.begintransaction (); //Create a Query objectQuery query = Session.createquery ("SELECT count (*) from User"); Object obj=Query.uniqueresult (); Long LBJ= (Long) obj; intCount =Lbj.intvalue ();                System.out.println (count);        Ts.commit (); } Catch(Exception e) {ts.rollback (); }finally{session.close ();        Factory.close (); }    }

Hibernate Frame Road (d) Hibernate Query method

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.