Hibernate (4) -- Comprehensive Analysis of One-to-Multiple Association Mappings

Source: Internet
Author: User

One-to-one and one-to-Two Mappings mentioned above are actually the same thing from the opposite perspective.

The principle of one-to-many and multi-to-one ing is the same. A foreign key is added at one end to point to one end. That is to say, in relational database tables, their tables and table fields are the same.

Their difference lies in the maintained relationship:

Multi-to-one maintenance relationship-the relationship with multiple points to one. If the relationship with multiple points to one is maintained, the load will be loaded.

One-to-multiple maintained relationships-one-to-multiple relationships. If one-to-multiple relationships are maintained, the one-to-one relationship will be loaded when multiple links are loaded.

Now let's use a one-to-many ing to describe the relationship between the student and the class.

1. hibernate one-to-many Association ing (Classes -----> Student)

Object classes. For a one-to-multiple relationship, a multi-end (student) needs to be loaded on one end (classes) during loading. Therefore, the entity classes must contain reference of the entity student.

Package com. lzq. hibernate; import java. util. Set; public class classes {private int ID; private string name; private set student; The getter and setter methods are omitted here}

Entity student

Package com. lzq. hibernate; public class student {private int ID; private string name; the getter and setter methods are omitted here}

The clasing file classes. HBM. xml. In a one-to-multiple relationship, the relationship must be maintained at one end. Therefore, add the "one-to-many" label on the classes side to maintain the relationship.

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Stuing file student. HBM. xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Although we have completed the ing, there are still some problems with the ing. See the following loading:

Public void testone2manyadd () throws exception {session = NULL; transaction Tx = NULL; try {session = hibernateutils. getsession (); Tx = session. begintransaction (); Student student1 = new student (); student1.setname ("James"); Session. save (student1); Classes classes = new classes (); classes. setname ("Advanced Training"); Set students = new hashset (); students. add (student1); classes. setstudent (students); Session. save (classes); Tx. commit ();} catch (exception e) {e. printstacktrace (); hibernateutils. closesession (Session );}}

The result throws a propertyvalueexception. The reason is simple: we do not maintain the relationship at multiple ends (student). That is to say, student does not know which class he belongs to. Therefore, when we save student, the classesid of The Link field is null. This limits that the foreign key must be allowed not to be blank. If the foreign key is set to "cannot be blank", it cannot be saved. This exception is thrown only when the foreign key is set.

How can this problem be solved? Of course, we can. HBM. "Not-null = 'true'" is removed from the xml configuration, which can also be saved. However, we can observe the generated SQL statement. At this time, an insert and update statement is executed. Obviously, it inserts the student information and then inserts the updated classesid field. This method is obviously unfriendly. In addition to this method, can we solve it in other ways?

Therefore, a two-way one-to-many Association ing is generated:

2. hibernate bidirectional one-to-Multiple Association ing (Classes <-----> Student)

Since it is a bidirectional one-to-many Association ing, you must add a reference to one end (classes) to the multiple end (student) so that student) for storage and loading.

Package com. lzq. hibernate; public class student {private int ID; private string name; private classes; the getter/setter method is omitted here}

Corresponding configuration changes:

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

In addition, since the two-way one-to-Multiple Association ing is generated to make up for the defect of one-to-many Association ing, It is avoided that programmers write programs at one end (classes) to load, we can use the reverse technique to set the loading failure from the classes side:

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Why is one-to-one association ing Not a one-to-many problem? If you understand the above, this question will be answered well. In multi-to-one association ing, since the relationship is maintained at multiple ends, loading from multiple ends is of course no problem.

To sum up, if multiple-to-one association ing is used, ing is often completed in two-way to make up for the problem of one-way loading.

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.