Seven mapping relationships for Hibernate

Source: Internet
Author: User
Tags new set


An association map is the mapping of an association relationship to a database in which one or more references are in the object model. One, hibernate many-to-one association mapping: is the "many" at the end of the foreign key, pointing to the one end.

For example, multiple students corresponding to a class, a number of users corresponding to a level, etc., are many-to-one relationship.

1. "Many" end entities add variables and Getter,setter methods that refer to "one" end entities.

For example, multiple students corresponding to a class, in the student entity class joined: Private Grade Grade;

2, "Many" end configuration file, Student.hbm.xml in the label settings: <many-to-one name= "Grade" column= "Gradeid" ></many-to-one> two, Hibernate one-way primary key correlation mapping: Just like the primary key for two entities, you don't need to add extra fields.

For example, a student corresponding to a social security number, a student corresponding to an account information, etc., are one-to-one correlation mapping.

1, the person entity to hold the Idcard reference. Add attributes to the person entity: Private Idcard Idcard;

2, person side maintenance relationship, so need to focus on the person's configuration file, Person.hbm.xml popular, that is, the ID of the person is both the primary key and the foreign key.

So you need to modify the ID tag in person: In layman's terms, tell the ID of the person to associate with the ID in which entity.

The foreign generation policy here obtains the identity of the associated object.

The property tag here is the associated object.

<id name= "id" >    <generator class= "foreign" >        <param name= "Property" >idCard</param>    </generator></id>

3. Add the label of the association relationship in Person.hbm.xml:

<one-to-one name= "Idcard" constrained= "true" ><one-to-one/>

The one-to-one tag here indicates how hibernate loads its associated objects, which, by default, load the relational field values according to the primary key, and load the associated objects based on the primary key of the peer.

The constrained= "true" here indicates the ID primary key of the current primary key person, or a foreign key, and a FOREIGN KEY constraint statement is generated, referring to the ID primary key of the corresponding end's primary key Idcard.

Note: The foreign mapping strategy preserves the associated objects while saving the person object, also known as Idcard, and has cascading relationships between them.

However, conversely, it is not possible, that is, if only the Idcard object is saved, the person object is not saved, because the relationship is maintained on the person side, and Idcard does not know the existence of the person at all. Primary Key Association mapping has drawbacks and is poorly maintained. Three, hibernate two-way one-to-one correlation mapping: from one end can be traced to the other end, from the other end can be found at this end, you need two-way correlation mapping.

For example, want to according to the person's information to find his account information, also want to based on the account information to find out which person's information and so on.

1, modify the object model, add a reference to the person in the Idcard.java entity, adding the attribute: Private person person, (in the Person.java entity also has Idcard reference OH)

2, modify the IdCard.hbm.xml file, add one-to-one tag.

<one-to-one name= "Person"/>

Note The 1:one-to-one tag does not affect storage, it only affects loading, does not modify the database, that is, no fields are added, and Many-to-one modifies the database, adding a field.

Note 2: Understand the crawl strategy: fetch= "Select" Controls the order in which data is fetched. Four, one-way single-to-one unique foreign Key association mappings: A pair of primary key association is not good maintenance, with foreign key association object is better. Principle: Many-to-one special case.

For example, it is also the case of person and Idcard.

1, Person.java is the entity has the Idcard reference, namely joins the attribute: private Idcard Idcard;idcard.java does not have the person reference.

2, configuration files: IdCard.hbm.xml are common attributes. Person.hbm.xml's primary key generation strategy needs to change: change back to:

<generator class= "native"/>

Use the label Many-to-one and unique: <many-to-one name= "Idcard" unique= "true"/>, which limits the uniqueness of the many end.

Note: You need to save the Idcard first. otherwise error. Five, two-way single-to-one unique foreign key association: if there is a requirement, you can also establish a two-way foreign key association.

1. Add a reference to the corresponding end in person and Idcard, that is, add the corresponding attribute.

2, Person.hbm.xml and Example (iv), modify the IdCard.hbm.xml file, add one-to-one tags:

<one-to-one name= "Person" property-ref= "Idcard" ></ONE-TO-ONE>.

Note: The Property-ref attribute in the one-to-one tag must be specified as the name of the relationship field. Six, hibernate unidirectional one-to-many association mappings: let one end maintain relationships, and many-to-one mapping principle is the same

is to add a foreign key to one end at the end of the many, and the difference is to maintain a different relationship:

    • Many-to-one maintenance relationship: Multi-point-to-one relationship, if the maintenance of a multi-point-to-one relationship, then the loading of one end of a side will be loaded at the end of A;
    • One-to-many maintenance relationship: a point-to-many relationship, if you maintain a point-many relationship, then load one end of the time will be loaded at the end of many.

For example, there are many students in a class.

1, Classes.java and Student.java each have two common attribute ID and name. Add a new attribute in classes: private Set students;

2, the mapping file modification: Classes.hbm.xml In addition to the normal attribute, add the set map:

<set name= "Students" >    <key column= "Classesid" ></key>    <one-to-many class= " Com.juyahong.hibernate.Student "/></set>

Note 1: One end of the "I" maintains the relationship.

Note 2:session.save (student), Session.save (classes), saves student and classes objects. You can save the data successfully, but you need to issue an extra UPDATE statement to maintain the relationship. Seven, hibernate two-way one-to-many association mappings: let the one end to maintain the relationship.

The main problem is solving a one-to-many-way association defect rather than demand-driven.

1. Add the classes reference in the Student.java entity class. Private Classes Classes;

2, Student.hbm.xml Add many-to-one tag: <many-to-one name= "Classes" column= "Classesid"/>. Classes.hbm.xml in the example (vi) on the basis of the addition of the reverse attribute inverse (later will maintain the work of the relationship to student).

<set name= "Students" inverse= "true" >    <key column= "Classesid" ></key>    <one-to-many class= "Com.juyahong.hibernate.Student"/></set>

Note 1: Save the classes first, then save the student.

Note 2: A one-to-many bidirectional association mapping method:

    • Using the key tag on one end of the collection allows you to add a foreign key to one end.
    • Use Many-to-one labels on multiple end

Note 3:key labels are consistent with the fields that are added to the Many-to-one label, otherwise data clutter occurs.

Note 4: A one-to-many correlation mapping usually maintains a relationship on a multi-side, which invalidates a single end:

------Inverse Reversal Property usage: Can be used on a one-to-many and many-to-many bidirectional associations, the inverse property defaults to False, that is, the relationship can be maintained on the local side, if the inverse is true, that means that the side does not maintain the relationship, to the other end to maintain the relationship, This side fails. It is a reversal of the control direction and only affects the storage.

------Cascade is an operational chain reaction (temporarily known). Eight, hibernate one-way many-to-many association mappings:

For example, the relationship between the student and the curriculum, the relationship between the user and the role is a many-to-many relationship.

1, User.java and Role.java entity class all have two common attribute ID and name, add attribute in User.java, private Set roles;

2. Modify the User.hbm.xml mapping file: Map collection:

<set name= "Roles" table= "T_user_role" >    <key column= "user_id"/> <many-to-many class=        " Com.juyahong.hibernate.Role "column=" role_id "/>    </key></set>
Nine, hibernate bidirectional many-to-many association mappings:

1, add new set of private set users in Role.java;

2, on the basis of example eight, modify the Role.hbm.xml file

<set name= "Users" table= "T_user_role" >    <key column= "role_id"/> <many-to-many class=        " Com.juyahong.hibernate.User "column=" user_id "/>    </key></set>

The above introduces seven kinds of mappings in hibernate (also including the mapping of the primary key and foreign key), in the learning of this piece is mainly able to understand the UML diagram, in these conditions, modify two files, a model class, there is a mapping file. This article mainly refers to the need to pay attention to the modification of the place and some of the attention of the knowledge point, and some understanding of things, I think understand these enough to sketchy, enough to get started, there is no need to all put the code up.

Through learning hibernate, let me again mention the knowledge on the Internet, the old knowledge points, put on the point of closure. Also learned through the object model to achieve the operation of the database, a further understanding of the object-oriented spirit, with the original learning design pattern, everything is on the basis of the object, the realization of simple and convenient and easy to maintain the method, benefit, look forward to further understanding and exploration.

Seven mapping relationships for Hibernate

Related Article

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.