Hibernate Session operation

Source: Internet
Author: User

1. Increase
@Test      Public void Add () {        configuration cfg=new  configuration (). Configure ();        Sessionfactory Factory=cfg.buildsessionfactory ();        Session session=factory.opensession ();        Emps emp=new Emps ("Cat", "female", 15,30,2000);        Session.save (EMP);        Session.close ();        Factory.close ();    }
2. Enquiry
@Test      Public void Query1 () {        configuration cfg=new  configuration (). Configure ();        Sessionfactory Factory=cfg.buildsessionfactory ();        Session session=factory.opensession ();        Emps emp= (Emps) session.get (Emps.  Class, 1005);        System.out.println (Emp.tostring ());        Session.close ();        Factory.close ();    }
Lazy Loading:

Get method: Call to issue SQL query immediately

Load method: Call does not emit SQL when we need to use this object to query

Query:

You can use the Uniqueresult () method when there is only one query result.

Emps emp= (Emps) Query.uniqueresult ();

Paging:

        Query query = Session.createquery ("from Emps");         // page        out // take results        from the beginning of the first few Query.setfirstresult (0);         // up to a few results        per page Query.setmaxresults (3);        List<Emps> list = Query.list ();    
3. Modify the table-Create transaction required
@Test      Public void Update () {        configuration cfg=new  configuration (). Configure ();        Sessionfactory Factory=cfg.buildsessionfactory ();        Session session=factory.opensession ();        Transaction ts=session.begintransaction ();        Emps emp= (Emps) session.get (Emps.  Class, 1005);        Emp.setage (+);        Session.update (EMP);        Ts.commit ();        System.out.println (Emp.tostring ());        Session.close ();        Factory.close ();    }
4. Delete
@Test      Public void Delete () {        configuration cfg=new  Configuration (). Configure ();        Sessionfactory Factory=cfg.buildsessionfactory ();        Session session=factory.opensession ();        Transaction ts=session.begintransaction ();         // deleted by ID;        Emps emp=New  Emps ();        Emp.setid (1019);        Session.delete (EMP);        Ts.commit ();        Session.close ();        Factory.close ();    }
5. Querying all Objects (HQL)

Hql:hibernate Query Language

@Test      Public void Query2 () {        configuration cfg=new  configuration (). Configure ();        Sessionfactory Factory=cfg.buildsessionfactory ();        Session session=factory.opensession ();         = Session.createquery ("from Emps"); // Emps is a class name        list<emps> list = query.list ();          for (Emps e:list) {            System.out.println (e);        }        Session.close ();        Factory.close ();    }
6. Criteria Query
@Test Public voidQuery3 () {Configuration cfg=NewConfiguration (). Configure (); Sessionfactory Factory=cfg.buildsessionfactory (); Session Session=factory.opensession (); //Criteria Query: Hibernate object-oriented query (no statement)Criteria Criteria=session.createcriteria (Emps.class); List<Emps> list =criteria.list ();  for(Emps e:list) {System.out.println (e);        } session.close ();    Factory.close (); }
7.SQL Query
@Test Public voidQuery4 () {Configuration cfg=NewConfiguration (). Configure (); Sessionfactory Factory=cfg.buildsessionfactory (); Session Session=factory.opensession (); SQLQuery Createsqlquery= Session.createsqlquery ("SELECT * from Emps"); Createsqlquery.addentity (Emps.class); List<Emps> list =createsqlquery.list ();  for(Emps e:list) {System.out.println (e);        } session.close ();    Factory.close (); }

Hibernate Session operation

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.