Hibernate (iii) -- Relationship ing in the framework

Source: Internet
Author: User

When designing a database, we will consider the relationship between tables, such as one-to-one, one-to-many, and many-to-many relationships that we often mention before. In the database, we use foreign keys, the third table to implement these relationships. How are the mappings between the Hibernate time entity class and the tables in the database mapped? Here is a brief summary:


One-to-many, multi-to-one relationship ing. Here we will give a simple demonstration of students and classes:


1. Compile the students' classes and corresponding ing files:


Private int sid; private String sname; private Classes classes; //, Class Object is introduced, multiple-to-one

 
  
   
  
  
  
  
 


2. The entity class of the class and the corresponding ing file:


Private int cid; private String cname; private Set
 
  
StudentSet = new HashSet
  
   
(); // Introduces a set of student classes, one-to-many
  
 

 
  
   
  
  
  
  
   
   
  
 

2. Self-Association: Associate yourself in your own class, such as the relationship between parent menu and sub-menu, corresponding entity class, And ing file.


Private int mid; private String name; private Set
 
  
MenuSet = new HashSet
  
   
(); // The relationship between the parent Menu and the Child Menu is: one-to-many private Menu pmenu; // The Relationship Between the child Menu and the parent Menu is: many-to-one
  
 

 
  
   
  
  
  
  
  
   
   
  
 

3. One-to-one relationship ing:

1. Assume that the primary key is one-to-one for ing: that is to say, the passive primary key is from the active primary key, which can also be called a foreign key:

Classes are added to each other.

Add one-to-one tags to the active side of the ing file:

 Passive, the primary key is a foreign key, and a one-to-one ing label is also added:
 
  
   User
  
 
 
 

2. Using Foreign key constraints is actually a special case of many-to-one,

Class.

In the ing file, the middle school student takes the initiative to add Foreign keys for constraints and add multiple-to-one labels,

 
 Class ing file: add one-to-one labels:
 

4. many-to-many relationships. For example, if a student maps to a course, a third table is generated in the database for maintenance:

Introduce the set of the other party to each class to indicate many-to-many.

Student end:

 
  
  
 

Course end:

 
 
  
  
 



5. Joint primary key ing. This situation is rare. Let's take a look,

When such a joint key occurs, we need to define a class for it to implement the Serializable interface. The class declares the attributes of multiple fields, indicating the fields of the joint primary key.

When configuring the ing file, the primary key configuration can use the tags of the joint primary key:

composite-id name="id">
 
 

In summary, there are several simple examples for setting ing for database associations in Hibernate. The main reason is that we can master the use of several corresponding labels based on the actual situation, and also use the attributes inside. Such as lazy, cascade, and fetch. Observe the differences. In this way, we can set the association, generate a database based on the class, and perform Association operations on the data in the database to make it more reasonable!


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.