Working with tables in Hibernate

Source: Internet
Author: User

Hibernate CRUD Testing

Working with tables in Hibernate, Add,load,update,delete,list,pager (paging)

 Packageorg.test.test;ImportJava.text.SimpleDateFormat;Importjava.util.List;Importorg.hibernate.Session;Importorg.junit.Test;ImportOrg.zttc.itat.model.User;Importorg.zttc.itat.util.HibernateUtil; @SuppressWarnings ("Unchecked") Public classTestcrud {Private Static FinalSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); @Test Public voidTestadd () {Session session=NULL; Try{Session=hibernateutil.opensession ();                        Session.begintransaction (); User u=NewUser (); U.setborn (Sdf.parse ("1977-11-22")); U.setnickname ("Zhang Lao Seven"); U.setpassword ("123"); U.setusername ("Zhanglaowu");                        Session.save (U);        Session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace (); if(session!=NULL) session.gettransaction (). rollback (); } finally{Hibernateutil.close (session); }} @Test Public voidTestLoad () {Session session=NULL; Try{Session=hibernateutil.opensession (); User u= (user) session.load (user).class, 2);        SYSTEM.OUT.PRINTLN (U); } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }} @Test Public voidTestupdate () {Session session=NULL; Try{Session=hibernateutil.opensession ();            Session.begintransaction (); User u= (user) session.load (user).class, 2); U.setnickname ("Zhang Xiao Si");            Session.update (U);        Session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace (); if(session!=NULL) session.gettransaction (). rollback (); } finally{Hibernateutil.close (session); }} @Test Public voidTestdelete () {Session session=NULL; Try{Session=hibernateutil.opensession ();            Session.begintransaction (); User u=NewUser (); U.setid (2);            Session.delete (U);        Session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace (); if(session!=NULL) session.gettransaction (). rollback (); } finally{Hibernateutil.close (session); }} @Test Public voidTestlist () {Session session=NULL; Try{Session=hibernateutil.opensession (); List<User> users = Session.createquery ("From User"). List ();  for(User u:users) {System.out.println (U); }        } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }} @Test Public voidTestpager () {Session session=NULL; Try{Session=hibernateutil.opensession (); List<User> users = Session.createquery ("From User"). Setfirstresult (0). Setmaxresults (2). List ();  for(User u:users) {System.out.println (U); }        } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }    }}

Working with tables in 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.