The mapping relationship of hibernate

Source: Internet
Author: User
Tags set set

1 Collection Mappings

One buyer corresponds to multiple addresses

Configuration of the Set collection

<!--The next thing we should configure is our set of collection mappings set--

<set name= "Addresses" table= "t_address" >

<!--This key is equivalent to configuring a foreign key so this foreign key is automatically mapped to the current class's primary key--

<!--this u_id will automatically map to the ID value of the primary key above--

<key column= "u_id" ></key>

<!--The following column is dedicated to storing addresses

Keep in mind that because it is a collection, the data type of the data in the collection must be written or otherwise error

-

<element column= "Address" type= "java.lang.String" ></element>

</set>

List collection Configuration

<!--The next thing we should configure is our collection map list--

<list name= "Addresses" table= "T_address_list" >

<!--is configured with our foreign key--

<key column= "u_id" ></key>

<!-- here is the Set Set unique difference The following configuration represents a sorted column -

<list-index column= "Indexs" ></list-index>

<!--is configured with a real-mapping field--

<element column= "Address" type= "java.lang.String" ></element>

</list>

Configuration of the Map collection

<!--Here's a map of the map collection--

<map name= "Addresses" table= "T_address_map" >

<key column= "u_id" ></key>

<map-key column= "keys_001" type= "java.lang.String" ></map-key>

<element column= "Address" type= "java.lang.String" ></element>

</map>

Attention:

The first thing to do when using a set map is to master the data types that must be given to the data in the collection otherwise error

2--in using our list collection is the time to give our list collection a sorted column (required)

3--you need to add a mapping field for the key in the collection mapping table when using the Map collection

4--in the collection <key></key> this represents the key that the collection is referencing, and the foreign key is the auto-complete mapping

The foreign key is actively mapped to the primary key of our entity.

One-to-many mapping configuration file:

<!--is configured with a one-to-many collection map--

<set name= "Emps" table= "T_employee" cascade= "Save-update,delete" >

The foreign keys in the <!--collection are automatically mapped to the primary key above--

<key column= "dept_id" ></key>

<one-to-many class= "employ"/>

</set>

2-to-one mapping configuration file:

<many-to-one name= "Dept" class= "dept" column= "dept_id" ></many-to-one>

Note: The actual development does not require both sides of the configuration, and generally in practice are more maintenance one

cascading issues Cascade

<set name= "Emps" table= "T_employee" cascade= "Save-update" >

The foreign keys in the <!--collection are automatically mapped to the primary key above--

<key column= "dept_id" ></key>

<one-to-many class= "employ"/>

</set>

CASCADE: Cascade Cascade with three types: Save and update save-update cascade Delete all (the union of the first two guys)

Cascading saves and updates are available, but in the development of the general situation of the lower-level deletion is not used because he will affect more than one table

4 Many-to-many association mappings

A programmer can develop multiple project programmers-----> projects (one-to-many relationships)

A project is developed by multiple developers-----> Programmers (one-to-many relationships)

In summary, two one-to-many relationships constitute a many-to-many relationship

The configuration of one side of project

<!--is configured with many-to-many collection mappings--

<set name= "Devs" table= "t_relation" cascade= "All" >

<!--This key you remember is the primary key to the current class of the map--

<key column= "pro_id" ></key>

<!--is configured with another table associated with it

Column: Configures the name of this value that the other's primary key maps in the relational table

Class: Configure the name of the other person

-

<many-to-many column= "dev_id" class= "Developer" ></many-to-many>

</set>

Configuration of the Developer party

<!--is configured with many-to-many collection mappings--

<set name= "Pros" table= "t_relation" cascade= "All" >

<!--This key you remember is the primary key to the current class of the map--

<key column= "dev_id" ></key>

<!--is configured with another table associated with it

Column: Configures the name of this value that the other's primary key maps in the relational table

Class: Configure the name of the other person

-

<many-to-many column= "pro_id" class= "Project" ></many-to-many>

</set>

5 Inheritance Mappings

1 Common Inheritance Mappings

Requirements: Now there are animals (animal this class) and the dog class and fish This class requires the use of inheritance mappings to complete the configuration of this association relationship

<class name= "Dog" table= "T_dog" >

<!--configuration is the primary key---in the table

<id name= "id" type= "Java.lang.Integer" >

<generator class= "Native" >

</generator>

</id>

<!--is configured for normal columns--

<property name= "Color" ></property>

<property name= "Name" ></property>

<property name= "num" ></property>

</class>

2 All self-class parent classes correspond to a single table

Classification: First there must be a classification only after the classification then this will know who is the data

The fields of the table should contain all the fields

<class name= "Animal" table= "t_animal_01" >

<!--configuration is the primary key---in the table

<id name= "id" type= "Java.lang.Integer" >

<generator class= "Native" >

</generator>

</id>

<!--Configure a non-public

<!--the first of the entire classification

Here's the column for the classification.

-

<discriminator column= "Type_" length= "$" type= "string" ></discriminator>

<!--is configured for normal columns--

<property name= "Color" ></property>

<property name= "Name" ></property>

<!--to configure our dog and fish separately--

<subclass name= "Dog" discriminator-value= "Dog_" >

<!--want to configure this child with a type--

<property name= "num" type= "string" ></property>

</subclass>

<!--configuring Fish--

<subclass name= "Fish" discriminator-value= "Fish_" >

<property name= "Fishcoatcount" type= "Java.lang.Integer" ></property>

</subclass>

</class>

3 All classes correspond to tables

1>: To implement the sub-table then the first thing to do is to anianl the primary key from the growth to me changed ...

2>: Using the Join-class to complete the sub-table

<class name= "Animal" table= "t_animal_02" >

<!--configuration is the primary key---in the table

<id name= "id" type= "string" >

<generator class= "Assigned" >

</generator>

</id>

<!--is configured for normal columns--

<property name= "Color" ></property>

<property name= "Name" ></property>

<!--the next step is to configure our sub-table--

<joined-subclass name= "Dog" table= "t_dog_02" >

<!--the key here automatically maps to the primary key of the class above--

<key column= "id" ></key>

<!--is configured with other columns--

<property name= "num" ></property>

</joined-subclass>

<!--next Configure our fish-->

<joined-subclass name= "Fish" table= "t_fish_02" >

<!--the key here automatically maps to the primary key of the class above--

<key column= "id" ></key>

<!--is configured with other columns--

<property name= "Fishcoatcount" ></property>

</joined-subclass>

</class>

4 The parent class does not correspond to the table, and the subclasses correspond

Why not use the first one? Because the first kind to write a lot of configuration files is not good to make ...

<!--abstract= "true": This indicates that it is not supposed to be a table--

<class name= "Animal" abstract= "true" >

<!--configuration is the primary key---in the table

<id name= "id" type= "string" >

<generator class= "Assigned" >

</generator>

</id>

<!--is configured for normal columns--

<property name= "Color" ></property>

<property name= "Name" ></property>

<!--The next step is configuring the Add-on table--

<union-subclass name= "Dog" table= "t_dog_03" >

<property name= "num" ></property>

</union-subclass>

<!--the fish is configured below--

<union-subclass name= "Fish" table= "t_fish_03" >

<property name= "Fishcoatcount" ></property>

</union-subclass>

</class>

6 One-to-one mapping

Requirements: Questions about people and identity cards

One person only has one I.D.

An identity card is not just a match for a person

Person-----> ID

Identity card------> personal Relationship

Configuration of the ID card

<class name= "Idcard" table= "T_idcard" >

<!--configuration is the primary key---in the table

<id name= "CardId" type= "Java.lang.Integer" >

<generator class= "Native" >

</generator>

</id>

<!--is configured for normal columns--

<property name= "Cardnum" ></property>

<!--The following is configured for this side of the reference primary key--

<many-to-one name= "People" column= "p_id" class= "People" cascade= "all" ></many-to-one>

</class>

Configuration of the People

<class name= "People" table= "T_people" >

<!--configuration is the primary key---in the table

<id name= "PId" type= "Java.lang.Integer" >

<generator class= "Native" >

</generator>

</id>

<!--is configured for normal columns--

<property name= "Name" ></property>

<!--Next you should configure one-to-one mapping--

<one-to-one name= "Idcard" class= "Idcard" cascade= "All" ></one-to-one>

</class>

Take the first primary key as the second primary key

The mapping relationship of hibernate

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.