One-to-Multiple Association ing (one to many + one-way)

Source: Internet
Author: User
One-to-many relationship ing)

Note: Load and get load data based on the primary key, not the primary key cannot be loaded. Hihernate one-to-many Association ing (one-way classes -----> Student) one-to-many Association ing utilizes the multi-to-one association ing principle and multiple-to-one association ing: when a foreign key is added to multiple ends to point to one end, the relationship maintained by the key is the one-to-one relationship ing. When a foreign key is added to multiple ends to point to one end, it maintains a one-to-many relationship, that is, the one-to-many and multi-to-one ing policies are the same, but the disadvantages of maintaining a one-to-one relationship between different sites are as follows: * If the classesid field in the t_student table is set to a non-empty value, it cannot be saved. * Because the relationship is not maintained at the student end, student does not know which class it belongs, Therefore, redundant update statements are required to update the relationship.

In the object model, a one-to-many Association is expressed by a set.For example, there is a one-to-many relationship between the class and students. We will take the class and students as an example to illustrate that the class needs to hold a set of students.


Step 1: first create an object link class:Class;

Public class classes { Private int ID; Private string name; Private set students; // hold a set of students in the class. set is used because the items in the set cannot be repeated. Public int GETID (){ Return ID; } Public void setid (int id ){ This. ID = ID; } Public String getname (){ Return name; } Public void setname (string name ){ This. Name = Name; } Public set getstudents (){ Return students; } Public void setstudents (set students ){ This. Students = students; } }

Student's:

Public class student { Private int ID; Private string name; Public int GETID (){ Return ID; } Public void setid (int id ){ This. ID = ID; } Public String getname (){ Return name; } Public void setname (string name ){ This. Name = Name; }}


Step 2: Create a ing file for the object:Student's ing file is:

<Hibernate-mapping> <Class name = "com. bjsxt. hibernate. Student" table = "t_student"> <ID name = "ID"> <Generator class = "native"/> </ID> <Property name = "name"/> </Class>


The class ing file is:

<Hibernate-mapping package = "com. bjsxt. hibernate"> <Class name = "classes" table = "t_classes"> <ID name = "ID"> <Generator class = "native"/> </ID> <Property name = "name"/> _________________________________________________________________________________________________________________ // The key lies in this: the set label is used to declare, and name is the attribute name. Students is declared above. <Set name = "Students"> <Key column = "classesid"/> // key tag. To add a field to a column name, the field classid is added to the student table. As a foreign key pointing to student, note that this field is added to the student table. <One-to-learn class = "student"/> // use the one-to-learn label to indicate one-to-multiple link ing, on the other hand, the class attribute is used to specify the type of elements in the set. Here, the elements of the // student type usually need to be added to the package path because they have been written in the package, therefore, you do not need to write the complete path. </Set> ___________________________________________________________________________________________________________________________ </Class>




Step 3: Write the hibernate configuration file:

<Hibernate-configuration> <Session-factory> <Property name = "hibernate. Connection. url"> JDBC: mysql: // localhost/hibernate_one2many_1 </property> <Property name = "hibernate. Connection. driver_class"> com. MySQL. JDBC. Driver </property> <Property name = "hibernate. Connection. username"> root </property> <Property name = "hibernate. Connection. Password"> bjsxt </property> <Property name = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </property> <Property name = "hibernate. show_ SQL"> true </property> <Mapping Resource = "com/bjsxt/hibernate/classes. HBM. xml"/> <Mapping Resource = "com/bjsxt/hibernate/student. HBM. xml"/> </Session-factory> Code : Note: we must first have a student and then put the student into the set, so we should first create a student.

Public class one2manytest extends testcase { Public void testsave1 (){ Session session = NULL; Try { Session = hibernateutils. getsession (); Session. begintransaction (); // Create a student first Student student1 = new student (); Student1.setname ("10 "); Session. Save (student1); // students need to save it first. At this time, the classid has no value. Student student2 = new student (); Student2.setname ("zuer "); Session. Save (student2); // at this time, the classid is also null. Set students = new hashset (); // use hashset Students. Add (student1 ); Students. Add (student2 ); Classes classes = new classes (); Classes. setname ("Shang xuexiang "); Classes. setstudents (students ); // It can be properly saved Session. Save (classes); // at this time, the classid has a value. Session. gettransaction (). Commit (); // You must issue two update statements consecutively to update the classid value in the student table. From this we can see that the classid is a null value at the beginning. // if we first set the classid attribute to a non-empty value, a problem will occur. It cannot be saved. So there is a certainty. When there are multiple data records, multiple update statements must be issued, which is time consuming. } Catch (exception e ){ E. printstacktrace (); Session. gettransaction (). rollback (); } Finally { Hibernateutils. closesession (session ); } } Public void testload1 (){ Session session = NULL; Try { Session = hibernateutils. getsession (); Session. begintransaction (); Classes classes = (classes) Session. Load (classes. Class, 1 ); System. Out. println ("classes. Name =" + classes. getname ()); Set students = classes. getstudents (); For (iterator iter = students. iterator (); ITER. hasnext ();){ Student = (student) ITER. Next (); System. Out. println ("student. Name =" + student. getname ()); } Session. gettransaction (). Commit (); } Catch (exception e ){ E. printstacktrace (); Session. gettransaction (). rollback (); } Finally { Hibernateutils. closesession (session ); } } }




































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.