Map based on List collection

Source: Internet
Author: User

1. Using the list collection in an entity class

1  Public class Grade {2     Private int ID; 3     Private String name; 4     Private New Arraylist<student> (0); 5 }

2. mapping files

1 Package= "Cn.siggy.pojo" >2<className= "Grade" >3<id name= "id" >4<generatorclass= "Native" ></generator>5</id>6<property name= "Name"/>7<list name= "Students" cascade= "all" >8<!--key indicates foreign key column name---9<key column= "grade_id" ></key>Ten<!--generate a column at one end to indicate the order if the column name is not indicated by default to IDX One values are maintained by Hibernate A- -<list-index column= "Sort" > -</list-index> the<!--One-to-many the type represented by students in a one-to-many class grade-- -<one-to-manyclass= "Student"/> -</list> -</class>

3. Test code

1 @Test2      Public voidTestsave ()throwshibernateexception, serialexception, sqlexception{3Configuration cfg =NewConfiguration (). Configure ();4Sessionfactory factory = Cfg.buildsessionfactory (NewStandardserviceregistrybuilder ()5 . Applysettings (Cfg.getproperties ()). build ());6Session session =NULL;7Transaction tx =NULL;8         Try{9Session =factory.opensession ();Tentx =session.begintransaction (); OneGrade Grade =NewGrade (); AGrade.setname ("Foundation"); -              -Student stu =NewStudent (); theStu.setname ("Zhang San Crazy"); -Stu.setage (22); -Student STU1 =NewStudent (); -Stu1.setname ("Lao Wang")); +Stu1.setage (23); -Student STU2 =NewStudent (); +Stu2.setname ("Lao Li"); AStu2.setage (23); at             //Associate - grade.getstudents (). Add (stu); - grade.getstudents (). Add (STU1); - grade.getstudents (). Add (STU2); -             //The order in which the data is saved is determined by the configuration of the foreign key. -             //If the foreign key cannot be null, then save one end first in             //If the foreign key can be null, then you can save the - Session.save (grade); to Session.save (stu); + Session.save (STU1); - Session.save (STU2); the              * tx.commit (); $             Panax Notoginseng}Catch(hibernateexception e) { -             if(tx!=NULL) the Tx.rollback (); + e.printstacktrace (); A             Throwe; the}finally{ + hibernateutil.closesession (); -         } $     } $ @Test -      Public voidTestget () { -Session session =NULL; theTransaction tx =NULL; -         Try{WuyiSession =hibernateutil.getsession (); thetx =session.begintransaction (); -             //Fetch Data WuGrade Grade = (Grade) session.get (Grade.class, 1); -System.out.println ("gradename=" +grade.getname ()); AboutSYSTEM.OUT.PRINTLN ("Data for multiple end of grade"); $list<student> list =grade.getstudents (); -              for(Student stu:list) { - System.out.println (Stu.getname ()); -             } A tx.commit (); +}Catch(hibernateexception e) { the             if(tx!=NULL) - Tx.rollback (); $ e.printstacktrace (); the             Throwe; the}finally{ the hibernateutil.closesession (); the         } -}

Map based on List collection

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.