Hibernate correlation Relationship Mapping configuration file

Source: Internet
Author: User

Vocabulary interpretation

Relationship : The State of interaction and interconnectedness between things. The largest range.

Contact : Represents the connection between entities and entities in a relational database, 1:1,1:n,m:n.

Association : Represents the relationship between objects, both quantitative and directional; verbs: connecting objects in some way.

mapping : This refers to a correspondence between a Java object and a database table. Verb: Form this correspondence.

Cascade : There is a relationship between the two sides of the action, the other side will also take some action.

Associated Types of contacts

Without regard to the direction of association, the connection is the relation between entity and entity in the relational database, 1:1,1:n,m:n.

Single-Contact (1:1): such as user and ID card, a husband and wife

One-to-many links (1:n): such as class and student

Many-to-many links (m:n): such as students and elective courses

The direction of the association

The direction of an association relationship can be divided into one-way and two-way associations.

The direction of bi-directional correlation is not important, since either side can maintain relationships. In other words: one-to-many and many-to-two are the same in a bidirectional association.

One-Way Association

In association marks such as <many-to-one> or <ONE-TO-MANY>, the direction is left to right, in other words, the relationship maintained by the left, see the example below.

Suppose there are two table person and address tables, and the relation between them is n:1;

That is, a person's residence address is unique, but an address can be more than one person to live.

If in the business logic of the application, only each person instance is required to be able to query for its corresponding address instance, and the address instance does not need to query for its corresponding person instance.

<class name= "person" table= "person" >    <id name= "id" >        <generator class= "native"/>    < /id>    <many-to-one name= "Address"        column= "Addressid"        not-null= "true"/></class>< Class Name= "Address" >    <id name= "id" column= "addressid" >        <generator class= "native"/>    </id></class>

Description

This is a one-to-one, Uni-Directional Association: Multiple parties maintain their relationships and need to include the associated tag,<many-to-one> within the class element of name= "person". This association is very much.

Suppose there are two table person tables and a Tel table, the connection between them is 1:n;

That is, a person's contact number may be multiple, a phone can only correspond to one person.

If in the business logic of the application, we are only concerned that each person instance can query all of its corresponding Tel instances, and the Tel instance does not need to query for its corresponding person instance.

<class name= "Person" >    <id name= "id" column= "personId" >        <generator class= "native"/>    </id>    <set name= "tels" >        <key column= "PersonId" not-null= "true"/>        <one-to-many class= "Tel"/>    </set></class><class name= "Tel" >    <id name= "id" column= "telid" >        <generator class= "native"/>    </id></class>

Description

This is a one-to-many one-way association: by one side to maintain their relationship, you need to include the association tag,<one-to-many> within the class element of name= "person". This association is relatively less. In most cases we are instances of a multi-party operation.

Bidirectional correlation

Configuring a one-way association on both sides constitutes a two-way management. In the actual development process, many times need two-way correlation, it is in the process of solving one-to-many maintenance relationship of the shortcomings played a certain role in repairing.

Suppose there are two table person and address tables, and the relation between them is n:1;

That is, a person's residence address is unique, but an address can be more than one person to live.

Each person instance is required to be able to query for its corresponding address instance, and the address instance needs to be queried for its corresponding person instance.

<class name= "Person" >    <id name= "id" column= "personId" >        <generator class= "native"/>    </id>    <many-to-one name= "Address"        column= "Addressid"        not-null= "true"/></class> <class name= "Address" >    <id name= "id" column= "addressid" >        <generator class= "native"/>    </id>    <set name= "People" inverse= "true" >        <key column= "Addressid"/>        < One-to-many class= "Person"/>    </set></class>

Description

This is a many-to-one bidirectional association. The relationship between the two sides is maintained. You need to include the association tag,<many-to-one> within the class element of name= "person". At the same time, a set mapping <set> is added within the class element of the Name= "Address", and the associated tag:<one-to-many> is added to it.

Association markers

In Hbm.xml, the association marks <one-to-one>, <many-to-one>, <one-to-many>, <many-to-many>, and the associated direction is left to right.

Association Tag Properties

Briefly introduce the following, except that name is mandatory and the rest is optional. More of our reference documents.

Name= "property name corresponding to this class"

column= "map field names to this table"

class= "Mapping entity classes to this table"

Unique= "Ture|false":(database foreign key field to generate a unique constraint)

Not-null= "Ture|false" defaults to False (whether the database foreign key field is allowed to be a null value)

lazy= "Ture|false" Default proxy (lazy loading)

About Cascade (cascading) Properties

Cascading means that you specify an action linkage between two objects, and after an object has been executed, the same action is required on the Cascade object to which it is specified.

Total values are: all, none, Save-update, delete

All-represents cascading operations in all cases

None-does not perform cascade operations in all cases

save-update-cascade Operations when saving and updating

delete-Cascade operation when deleting

Collection Map Markers <set>

<set name= "Peoples" inverse= "true" >    <key column= "Addressid"/> <one-to-many class=    "Person"/ ></set>

<set name= "Peoples" inverse= "true" >name the property name for the collection of persisted objects.

<key column= "ClassID" > Column name of the foreign key

<one-to-many class= "cn.edu.bzu.hibernate.Student"/> Class persistence class

About the Inverse property

Control inversion, mainly used in a one-to-many, multi-pair bidirectional association, inverse can be set to <set>, the default inverse is false. True indicates reversal, which is the responsibility of the other side, does not reverse, own responsibility, if not set, the one and many both sides are responsible for control, therefore, will throw a duplicate SQL statement and repeatedly add data.

Who is more who is a

We say one to the other, many pairs, many to many who is to whom? In the object association tag in the class element in the map file (. hbm.xml), such as <many-to-one>

Then the class element's property name is the many,<many-to-one> tag in which name is one. At the same time column= "Addressid" indicates the foreign key of the person table.

<class name= "person" table= "person" >    <id name= "id" >        <generator class= "native"/>    < /id>    <many-to-one name= "Address"        column= "Addressid"        not-null= "true"/></class>

In the example above, the person is many,address is one.

You can understand this <many-to-one> who is the many,<many-to-one> attribute name is one.

One-way associative hbm.xml configuration

One-way association is commonly used for many-to-one

One-way many-to-one associations are the most common one-way relationships. This association is a multiple-to-one in a database-relational pattern:

A foreign key for this table refers to the primary key field of the target table.

The person in the following example is in contact with address (database term) as N:1, so you can understand:

That is, a person's residence address is unique, but an address can be more than one person to live.

<class name= "person" table= "person" >    <id name= "id" >        <generator class= "native"/>    < /id>    <many-to-one name= "Address"        column= "Addressid"        not-null= "true"/></class>< Class Name= "Address" >    <id name= "id" column= "addressid" >        <generator class= "native"/>    </id></class>

Note column= "Addressid" in the:<many-to-one> tag, adding a foreign key addressid to the person table.

SQL output:

CREATE TABLE person (personId bigint not null primary key, addressid bigint NOT NULL) CREATE TABLE Address (Addressid bi Gint NOT NULL primary key)
Bidirectional Association Hbm.xml Configuration

1. One-to-many/multi-pair

Bidirectional many-to-one association is the most common association relationship. The following example explains the parent/Child Association of this standard.

The person in the following example is in contact with address (database term) as N:1, so you can understand:

That is, a person's residence address is unique, but an address can be more than one person to live.

<class name= "Person" >    <id name= "id" column= "personId" >        <generator class= "native"/>    </id>    <many-to-one name= "Address"        column= "Addressid"        not-null= "true"/></class> <class name= "Address" >    <id name= "id" column= "addressid" >        <generator class= "native"/>    </id>    <set name= "People" inverse= "true" >        <key column= "Addressid"/>        < One-to-many class= "Person"/>    </set></class>

Note: The Many-to-one association needs to add one end to inverse= "true"; column= "Addressid" indicates the foreign key of the person table.

SQL output:

CREATE TABLE person (personId bigint not null primary key, addressid bigint NOT NULL) CREATE TABLE Address (Addressid bi Gint NOT NULL primary key)

2. One-to-one

A two-way, one-to-one association based on foreign key associations is also common.

In the following example, the contact of person to address (database language) is 1:1, so to understand:

That is, a person can only manage a place, a place can only be managed by one person.

Column= "Addressid" indicates the foreign key of the person table.

<class name= "Person" >    <id name= "id" column= "personId" >        <generator class= "native"/>    </id>    <many-to-one name= "Address"        column= "Addressid"        unique= "true"        not-null= "true"/ ></class><class name= "Address" >    <id name= "id" column= "addressid" >        <generator Class = "Native"/>    </id>    <one-to-one name= "person"        property-ref= "Address"/></class>

SQL output:

CREATE TABLE person (personId bigint not null primary key, addressid bigint NOT null unique) CREATE TABLE Address (addre SsId bigint NOT null primary key)
Associating with a connection table

Associations that use join tables are typically for many-to-many. The join table is a separate table in the data that stores the relationships of two entities.

This table has three fields: its own ID, and two foreign keys are related to the primary key of the two entities, respectively.

The following is the link between student and course (database terminology) is m:n, so to understand:

One student can choose multiple courses, and a course also has multiple students.

<class name= "Student" >    <id name= "id" column= "studentid" >        <generator class= "native"/>    </id>    <set name= "courses" table= "Studentcourse" >        <key column= "StudentID"/>        <many-to-many column= "CourseID"        class= "Course"/> </set></class><class    name= "Course" >    <id name= "id" column= "CourseID" >        <generator class= "native"/>    </id>    < Set name= "Students" inverse= "true" table= "Studentcourse" >        <key column= "CourseID"/>        < Many-to-many column= "StudentID"        class= "Student"/>    </set></class>

Note that:<set> adds the table property, and therefore tables are created separately.

SQL output:

CREATE TABLE Student (StudentID bigint not NULL primary key) CREATE TABLE Studentcourse (StudentID bigint not NULL, cour SeId bigint NOT NULL, PRIMARY key (StudentID, CourseID)) CREATE TABLE Course (CourseID bigint not null primary key)

Hibernate correlation Relationship Mapping configuration file

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.