Hibernate in session,hql, persistent, one-to-many, many-to-a

Source: Internet
Author: User

Package com.hanqi.test;
Import Java.util.Date;
Import java.util.List;

Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import Org.hibernate.boot.registry.StandardServiceRegistryBuilder;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;

Import Com.hanqi.entity.Course;
Import Com.hanqi.entity.Teacher;
Import Com.hanqi.entity.User;

public class Test01 {
Private Sessionfactory Sf=null;
Private Session Se=null;
Private Transaction ts;

Methods that are automatically executed before test cases are executedCommon objects that are typically used for initializationPredecessor Method @BeforePublicvoidInit){1 Get config file configuration cfg=New Configuration (). Configure ();2 Registering the configuration serviceregistry sr=New Standardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build ();3 Get Sessionfactory (equivalent to JDBC) SF =cfg.buildsessionfactory (SR); System.Out.println (SF);4 Generating session se=sf.opensession ();5 Start transaction ts=se.begintransaction ();}Post-placement methodTypically used to release resources @AfterPublicvoidDestroy){7 Commit Transaction ts.commit ();8 Releasing resources se.close (); Sf.close (); }Test Hibernate Connection Database @TestPublicvoidTest) {1 Get config file configuration cfg=New Configuration (). Configure ();2 Registering the configuration serviceregistry sr=New Standardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build ();3 Get Sessionfactory (equivalent to JDBC) sessionfactory SF =cfg.buildsessionfactory (SR); System.Out.println (SF);4 Generating Session session se=sf.opensession ();5 Start transaction ts=se.begintransaction ();6 Operational transactionsAdd dataThe instantiated new object is in a temporary state User u1=New User (); U1.setbirthday (New Date ()); U1.setmoney (2000); U1.setpassword ("123456"); U1.setusername ("Test One");Save dataThe object is transferred from the temporary state to the persisted state Se.save (U1) by the Save method; System.Out.println (U1); }Test Query Method @TestPublicvoidTest1 (){Querying dataTwo parameters are available:1 An instance of which persisted class needs to be returned2 Identification of the instance (primary key value of the data)The object obtained by the Get method of the session is in the persisted state of user u2= (user) se.Get (User.class,3); U2.setusername ("Modified"); System.Out.println (U2);Delete Se.delete (U2);Causes the persisted object to enter the delete state}@TestPublicvoidTest2 () {User u1= (user) se.Get (User.class,4); System.Out.println (U1); User u2= (user) se.Get (User.class,4);Load the data System now.Out.println (U2);} @TestPublicvoidTest3 () {User u1= (user) Se.load (user.class,4);Lazy loading System is loaded lazily.Out.println ("Dsad"); System.Out.println (U1);Load only when used}Test Free State @TestPublicvoidTest4 (){The object that gets the persisted state is the user u1= (user) se.Get (User.class,4); System.Out.println ("u1=" +u1);Close session se.close ();The U1 into a Free State u1.setusername ("Test free");Place the OID into null u1.setuserid (NULL);Re-create session se=sf.opensession ();Re-open transaction ts=se.begintransaction (); Se.saveorupdate (U1); System.Out.println (U1); }Test hql@TestPublicvoidTEST5 (){Using HQL1 Create a Query object query Qu=se.createquery ("From User where UserID;? and "+"UserName like ' Test% ' order by UserID");Set Placeholder Qu.setinteger (0,10);Execute Query list<user> lu=qu.list ();Method Chain CallLu=se.createquery ("from User where UserID;?"). Setinteger (0, ten). List ();for (User e:lu) {System.Out.println (e); }}Test paging @TestPublicvoidTest6 (){Set start line number: page =2(page-1) * Number of rows per page list<user> lu=se.createquery (From the User order by UserID). Setmaxresults (5). Setfirstresult (5). List ();for (User e:lu) {System.Out.println (e); }}Group Query @TestPublicvoidTEST7 () {List<object>lo=se.createquery ("Select UserName from the User group by UserName"). List ();for (Object O:lo) {System.Out.println (o); }}Test projection Query @TestPublicvoidTest8 () {List<object[]>lo=se.createquery ("Select Userid,username from User"). List ();For (object[] o:lo) {System.Out.println (o[0]+"" +o[1]); } System.Out.println ("Dasdasadfa");Returns a collection of persisted objects list<user> Lu=se.createquery ("Select New User (userid,username) from user"). List ();For (User u:lu) {System.  OUT.PRINTLN (U); }}//test Many-to-one @test public void test10 () {Course cs= (Course) se.get (Course.class,"9-888") ; System. Out.println (CS);} //test one-to-many @test public void test11 () {Teacher tc= (Teacher) se.Get (Teacher.class, "831"); System. OUT.PRINTLN (TC);}                 

}

Hibernate in session,hql, persistent, one-to-many, many-to-a

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.