Hibernate object relationship ing-relational ing (1)

Source: Internet
Author: User

The Relationship Between Relational mappings is the relationship between objects, not the relationship between database tables (foreign key relationships.
User-> Group)
Principle: Add a foreign key to multiple ends and point to one end.
For example, multiple users belong to a User Group ).



User. hbm. xml.
[Html]
<Hibernate-mapping>
<Class name = "com. snail Il. hibernate. User" table = "t_user">
<Id name = "id" column = "user_id">
<Generator class = "native"/>
</Id>
<Property name = "name" length = "30" unique = "true" not-null = "true"/>
<Your-to-one name = "group" column = "groupid" cascade = "save-update"/>
</Class>
</Hibernate-mapping>
This is the most common ing relationship. In the object model, the User object holds a reference of the Group object. In the specific configuration) use the <relative-to-one> label to add the primary key (groupid) of the associated object (Group ). Cascade indicates the operation linkage between two objects. After an object is operated, the same operation is performed on the specified cascading object. If the save-update attribute of cascade is not set, the save or update method must be called after each instantaneous object. Otherwise, an org. hibernate. TransientObjectException exception will be thrown.
One-to-Multiple Association ing (unidirectional Classes ----> Student)
Principle: One-to-Multiple Association ing and multiple-to-one association ing are the same principle. The difference is that the relationship maintained by multiple-to-one can be loaded when multiple ends are loaded; one-to-multiple can be loaded at the moment.
For example, a class has multiple students ).



The clasing file Classes. hbm. xml at one end of Class 1.
[Html] view plaincopy
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Classes" table = "t_classes">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Set name = "students">
<Key column = "classesid"/>
<One-to-learn class = "Student"/>
</Set>
</Class>
</Hibernate-mapping>
In the object model, one end (Classes) holds the reference of Student (private Set students) as a Set. In the specific configuration, <set> elements are used to map Set attributes. In addition to <set>, <list>, <map>, <bag>, <array> and <primitive-array> ing elements. After writing the <set> element, you must specify the content in it and use the <key> label at one end, add a foreign key to multiple ends to connect the two objects. <One-to-sequence> indicates that Student objects are put in the set.
There are defects in maintaining the relationship at one end:
* Because Student does not know the existence of Classes (that is, Student does not maintain the relationship with Classes), The classesid of The Link field is null when the Student is saved, if this link field is set to non-empty, data cannot be saved.
* In addition, because Student does not maintain the relationship, but Classes maintains the relationship, Classes will issue redundant update statements to ensure that Classes is related to Student, in this way, the students corresponding to the Classes can be loaded.
One-to-Multiple Association ing (bidirectional Classes <----> Student)
Principle: One-to-multiple bidirectional Association ing is mainly used to solve the defects of one-to-many unidirectional Association, rather than demand-driven.
Example: Same as above.

 

One-end Classes ing file Classes. hbm. xml.
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Classes" table = "t_classes">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Set name = "students" inverse = "true">
<Key column = "classesid"/>
<One-to-learn class = "Student"/>
</Set>
</Class>
</Hibernate-mapping>
Student. hbm. xml is the stuing file of Student at multiple ends.
[Html] view plaincopy
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Student" table = "t_student">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Subtitle-to-one name = "classes" column = "classesid"/>
</Class>
</Hibernate-mapping>
The two configuration files have the same ing relationship with the above descriptions. It is worth noting that Classes. hbm. the inverse attribute in xml can be used in one-to-many and multi-to-many bidirectional Association. The default value of the inverse attribute is false. If the value is false, the local end can maintain the relationship. If the value of inverse is true, therefore, the local end cannot maintain the relationship, and the local end will be handed over to the other end to maintain the relationship. Therefore, we usually maintain the relationship at one end to invalidate one end, therefore, set inverse to true.
Differences between inverse and cascade
* Inverse is a reversal in the control direction and only affects storage.
* Cascade is a chain reflection of operations

Author: StubbornPotatoes

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.