Hibernate05--list and iterator

Source: Internet
Author: User

/** *  * @authorSmall Tofu * After you, will thank you for your efforts now! Efforts! Insist! Don't give up! */ Public classstudenttest {Configuration Configuration=NULL; Sessionfactory sessionfactory=NULL; Session Session=NULL; Transaction Transaction=NULL; //test methods have the same code block@Before Public voidbefore () {//01. Load configuration file (Hibernate.cfg.xml) must be located in src root directoryConfiguration =NewConfiguration (). Configure (); //02. Create a session factory set into singleton modeSessionfactory =configuration.buildsessionfactory (); //03. Session HttpSession (user session) created through the factorySession =sessionfactory.opensession (); //04. Using object-oriented thinking to manipulate database additions and deletions must open transactionsTransaction =session.begintransaction (); } @After//Close Session     Public  voidAfter () {//no session in query does non-null validation        if(session!=NULL) {            //05. Close SessionSession.close (); }    }    /*** Hql:hibernate's query Language! * * The steps to execute the HQL statement: * 01. Get session * 02. Write HQL statement * 03.session.     Creatquery (HQL); Create a Query Object * 04. Execute Queries * * * LIST (): Whether we clear the cache or not, simply query the database to generate a SELECT statement if it is a list ()! */@Test Public  voidtestlist () {//String sql= "select * from Stu"; Stu is the table nameString hql= "from Student";//Student must be a class name//get the Query objectQuery query =session.createquery (HQL); //Start QuerySystem.out.println ("**************"); List List= Query.list ();//there is only one SQL statementSystem.out.println ("**************");  for(Object object:list) {System.out.println (object); } session.clear (); //Empty Cache//whether we clear the cache or not, as long as the list () queries the database to produce a SELECT statement immediatelyList list2=query.list (); }        /*** Iterator (): * 01. Execute N+1 Query statement without caching * First query ID statement in query.iterate (); Time to produce!     * The remaining n statements are generated at Iterate.hasnext ()! * N represents the total number of records in the database*/@Test Public  voidTestiterator () {String hql= "from Student";//Student must be a class nameQuery query =session.createquery (HQL); System.out.println ("**************"); Iterator Iterate=query.iterate (); System.out.println ("**************");  while(Iterate.hasnext ()) {Object Object=(Object) iterate.next ();        System.out.println (object); }    }        /*** 02. There is a cache situation * Query all student information!     * Iterate (): Execute an SQL statement! */@Test Public  voidTestIterator2 () {String hql= "From Stu"; Query Query=session.createquery (HQL); List List= Query.list ();//now that the SQL statement has been generated, all student are already in the session cache.System.out.println ("*********************"); Iterator Iterate=query.iterate (); System.out.println ("*********************");  while(Iterate.hasnext ()) {Object Object=(Object) iterate.next ();        System.out.println (object); }    }        /*** Get: Generate 1 Strips * iterate (): Generate 1 * iterate.hasnext (): 3 Results*/@Test Public  voidTestIterator3 () {Student stu= (Student) session.get (Student.class, 1);//now the cache has an object with ID 1String hql= "from Student"; Query Query=session.createquery (HQL); Iterator Iterate=query.iterate (); System.out.println ("*********************");  while(Iterate.hasnext ()) {Object Object=(Object) iterate.next ();        System.out.println (object); }    }}

Hibernate05--list and iterator

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.