Hibernate-----One-to-many association mappings (map File mode)

Source: Internet
Author: User

One-to-many and many-to-one mapping principle: all at the end of a multi-side to join a foreign key point to one end

The difference is that the maintenance relationship differs:

1 , many-to-one maintenance relationship is a multi-point relationship , if you maintain a multi-point relationship, load it up and load it up .

2 , a one-to-many maintenance relationship is a point-to-many relationship , if you maintain a point-to-many relationship, load one more time and load it up .

There is a flaw in maintaining relationships at one end (a one-to-many bidirectional correlation mapping can be used to resolve a defect in the maintenance relationship at one end):

1 Because many end student do not know classes The existence of span style= "color: #3F5FBF" (That is, student There is no maintenance relationship with classes sudent classes_id null

2 , in addition because Student do not maintain relationships, Classes Maintain relationships, Classes it will send out the extra Update statement, guaranteeing Classes and the Student It's related, so load Classes before you can put the Classes the corresponding students are loaded up

One, one-way association mappings

One ( maintenance side:Classes)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
To

Many ( non-maintenance side:Student)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

public void Testonetomany () {session session = NULL;           try {session = Hibernateutils.getsession ();            Session.begintransaction ();           The association has the directionality, one to many (depends on many), therefore first has the Student, then has classes Student student1 = new Student ();           Student1.setname ("name");            Session.save (STUDENT1);           Student Student2 = new Student ();           Student2.setname ("name");            Session.save (Student2);           set<student> set = new hashset<student> ();           Set.add (STUDENT1);            Set.add (Student2);           Classese classes = new Classese ();           Classes.setname ("ClassName");            Classes.setstudents (set);           Finally save the maintenance end Session.save (classes);        First insert two student//and then insert a classes//and then modify the two student (for maintaining the relationship) session.gettransaction (). commit ();           } catch (Exception e) {e.printstacktrace ();       Session.gettransaction (). rollback (); } finally {          Hibernateutils.closesession (session); }    }



Two, two-way association mapping

One ( maintenance side:Classes)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
To

Many ( maintenance side:Student)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">


Save one end and save many more to avoid multiple update statements

Inverse property (default false: The local side can maintain relationships; true: This side does not maintain relationships and is left to the other end) can be used on one-to-many and many-to-many bidirectional associations

A one-to-many correlation mapping usually maintains relationships at the end of a multi-side relationship, invalidating a single end (setting inverse=true)

Inverse PK Cascade

Inverse is the reversal of the control direction, affecting only the storage

Cascade is an operational chain reaction (synchronizing associated objects)



Hibernate-----One-to-many association mappings (map File mode)

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.