Hibernate frame additions and deletions change

Source: Internet
Author: User

1  PackageCn.happy.util;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.cfg.Configuration;6 7 /**8 * 1.1 Tool classes for the production of Session objects9  */Ten  Public classHibernateutil { One     Private StaticConfiguration cfg=NewConfiguration (). Configure (); A     Private StaticSessionfactory factory=cfg.buildsessionfactory (); -     //method to return to session -      Public StaticSession getsession () { the         returnfactory.opensession (); -     } -     //Close Session -      Public Static voidclosesession () { + getsession (). Close (); -     } +  A}

1  Packagecn.happy.entity;2 /**3 * Entity class4  * @authorAdministrator5  *6  */7  Public classStudent {8     PrivateInteger ID;9     PrivateString name;Ten     PrivateInteger age; One      A @Override -      PublicString toString () { -         return"Student [id=" + ID + ", name=" + name + ", age=" + Age + "]"; the     } -      PublicInteger getId () { -         returnID; -     } +      Public voidsetId (Integer id) { -          This. ID =ID; +     } A      PublicString GetName () { at         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; -     } -      PublicInteger getage () { in         returnAge ; -     } to      Public voidsetage (Integer age) { +          This. Age =Age ; -     } the  *}
1  Packagecn.happy.test;2 3 Importorg.hibernate.SessionFactory;4 Importorg.hibernate.Transaction;5 Importorg.hibernate.cfg.Configuration;6 Importorg.hibernate.classic.Session;7 Importorg.junit.Test;8 9 Importcn.happy.entity.Student;Ten /** One * Increase A  * @authorAdministrator -  * -  */ the  Public classAddtest { - @Test -      Public voidaddtest () { -Student stu=NewStudent (); +Stu.setid (1); -Stu.setname ("Zhang San"); +Stu.setage (18); AConfiguration cf=NewConfiguration (). Configure ("Hibernate.cfg.xml"); atSessionfactory factory =cf.buildsessionfactory (); -Session session =factory.opensession (); -Transaction tx =session.begintransaction (); -         //Save - Session.save (stu); - tx.commit (); in session.close (); -     } to  +      -}
1  Packagecn.happy.test;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.Transaction;5 Importorg.junit.Test;6 7 Importcn.happy.entity.Student;8 ImportCn.happy.util.HibernateUtil;9 /**Ten * Delete One  * @authorAdministrator A  * -  */ -  Public classDeltest { the @Test -      Public voiddeltest () { -Session session =hibernateutil.getsession (); -Student stu=NewStudent (); +Stu.setid (1); -Transaction tx =session.begintransaction (); + Session.delete (stu); A tx.commit (); at hibernateutil.closesession (); -System.out.println ("Del ok!"); -     } -  -}
1  Packagecn.happy.test;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.Transaction;5 Importorg.junit.Test;6 7 Importcn.happy.entity.Student;8 ImportCn.happy.util.HibernateUtil;9 /**Ten * Enquiry One  * @authorAdministrator A  * -  */ -  Public classGettest { the @Test -      Public voidgettest () -     { -Session session =hibernateutil.getsession (); +Student stu= (Student) session.get (Student.class, 1); -Transaction tx=session.begintransaction (); + System.out.println (stu); A     } at  -}
 Packagecn.happy.test;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;Importcn.happy.entity.Student;ImportCn.happy.util.HibernateUtil;/*** Change *@authorAdministrator **/ Public classupdatetest {@Test Public voidUpdatetest () {Session session=hibernateutil.getsession (); Student Stu= (Student) session.load (Student.class, 1); Stu.setname ("Lee Short"); Transaction TX=session.begintransaction ();        Session.update (Stu);        Tx.commit ();        Hibernateutil.closesession (); System.out.println ("OK"); }}

Hibernate frame additions and deletions change

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.