"SSH Basics" on hibernate relational Mapping (3)

Source: Internet
Author: User

Following the previous blog

One-to-many association mappings (unidirectional)

The above we introduced a lot of one, we turn to see a pair of more than one? So do you want to use a different mapping? What's the difference? The one-to-many and multi-pair mapping principles are consistent and the storage is the same. That is, the table of the generated database is the same. The difference between them is that the maintenance relationship is different.

The difference between them is that the maintenance relationship is different.

* Many-to-one maintenance relationship is: a multi-point relationship. With this relationship, it is possible to load a load when it is loaded.

* One-to-many maintenance relationships are: a point-to-many relationship. With this relationship, you will be able to load multiple loads when loading one.

A class has multiple students who can see the student information through the class.


Classes The mapping Relationship


Student The mapping Relationship


and look at the saved code. : set up students for the class, and because students maintain the relationship. What is the problem with saving the class ID at the student's end ?

public void TestSave2 () {Session session=null;try{session=hibernateutils.getsession (); Session.begintransaction (); Student student1=new Student (); Student1.setname ("Zhang San"); Session.save (STUDENT1); Student student2=new Student () student2.setname ("John Doe");//Save Student Session.save (STUDENT2); Classes classes=new Classes (); Classes.setname ("Power Node"); Set students=new HashSet (); Students.add (student1); Students.add (Student2); classes.setstudents (students); Session.save (classes); Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); Finally{hibernateutils.closesession (session);}}

There is a moment when the data is saved: So ? Problems brought about by



1 , students can be seen from the class. It is the class to maintain the relationship, not the students to maintain the relationship, students do not know which class they are. Therefore, the class ID does not know when the student is saved. To update which class the student is in, send out very many update statements to tell the student which class.

2 . When we set classesidnot-null= "true", we will not be able to save the data, the resolved method we changed to bidirectional Association mapping.

One-to-many association mappings (bidirectional)

In order to solve a one-to-many problem, we use a two-way pair. Each party is able to maintain each other.

Rather than demand-driven

One-to-many bidirectional correlation mapping:

* Use <key> tags on one end of the collection. Add a foreign key at one end of the multiple.

* Use <many-to-one> labels on multiple end

! ~ Note <key> tags and <many-to-one> tags add fields consistently, otherwise data clutter will result.

Classes Mapping Files


Student Mapping Files


Note: the Inverse property

1, the inverse Chinese meaning is opposite, reverses. In hibernate, inverse can be used on a one-to-many and many-to-many bidirectional association, and inverse defaults to false. False indicates that the local side can maintain the relationship, assuming that inverse is true, then the local side cannot maintain the relationship. will be handed over to one end to maintain the relationship. This side fails, so in a one-to-many association map we usually maintain relationships at one end. Let one end fail.

2, Inverse is the reversal of control direction, only affect the storage.

Compared to one-to-many unidirectional and bidirectional mappings, there is no difference from the storage structure, but it is seen from the configuration file. A one-to-many bi-directional configuration file has a <many-to-one> related configuration on a multiple-side profile, which guarantees a multi-pair mapping.

The next article continues.



"SSH Basics" on hibernate relational Mapping (3)

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.