Relationship design of Java EE User, Department, role, authority entity and Hibernate mapping Profile Relationship Summary

Source: Internet
Author: User


the project files in the article have been put on GitHub, welcome everyone to Follow,star, Address: Https://github.com/lawlite19/SmartEducation

A: Design entity, I am here through the UML design, and then directly can generate Java entity classes (easy to see the relationship of each class)

(1) User login class users and User details class Userdetails is a one-to-one relationship;

(2) User details class Userdetails and departments for many-to-one relationship; (multiple users in a department)

(3) Departmental categories are self-related and have superior departments;

(3) User-specific class userdetails and role-class roles are many-to-many relationships; (one user can have multiple roles, one role can also correspond to multiple users)

(4) Role class roles and permission class privilege classes are many-to-many relationships; (a role can have multiple permissions and one permission can correspond to multiple roles)

(5) The permission class is self-correlating because it is divided into several levels of permissions.


And then you can go around Java entity classes directly,



Then copy to MyEclipse, generate a Get, set method

Two: Write Hibernate's hbm.xml mapping file, the normal attribute segmentation on both screens to write on the line, The main entity is the corresponding relationship between entities


(1) The user class and the user information class is one-to-one, I use the Foreign Key Association (that is, the user table contains the user information table of the primary key ID)

In the user information class, there is a users attribute: (Cascade is cascading)

The Userdetails attribute in the user table indicates that the column name colum= "userdetails_id" When the database table is created


(2) Departmental and user information classes are many-to-one

There is a department attribute in the user information class that corresponds to the ID of the storage department in the table:


There is a userdetails attribute in the Department category, and key is the specified column:


(3) The User information class and the role class are many-to-many

The roles attribute in the user information class indicates that the intermediate table is T_userdetails_role: (Many-to-many relationships need to create an intermediate table that stores the primary key ID of two tables respectively)

Key indicates the corresponding column,

The Userdetails attribute is in the role class


(4) Also many to many, with the same



(5) The Department is self-related, the superior department is many to one, the subordinate department is a pair of many

(6) Permissions are self-correlating, same as above


Three: test, create a table when creating a sessionfactory, write a junit test execution



(1) The Discovery table is automatically created successfully, the following we mainly check whether the relationship corresponds to the correct


(2) correct


(3) correct


(4) correct


(5) correct


(6) correct


(7) correct


(8) correct


Iv. Summary

Mainly summarizes the correspondence of the relationship:

(1) One -to-one (foreign key)
Main Table
<many-to-one name= "" class= "" column= "" unique= "true" >
Sub-table
<one-to-one name= "" class= "" cascade= "All" ></one-to-one>

(2) A pair of multiple
<set name= "" >
<key column= "" ></key>
<one-to-many class= ""/>
</set>

(3) Many-to-one
<many-to-one name= "" class= "" column= "" >
</many-to-one>

(4) Many-to-many
<set name= "" table= "" >
<key column= "" ></key>
<many-to-many class= "" column= "" ></many-to-many>
</set>

One-to-many and many-to-many have set, many-to-many, and more table and column properties

Steps:

1, write the annotation correspondence relation

namely: xx attribute, this class and YY class of ZZ relation

2. Copy the template above

3, fill in: (1) name---->XX

(2) class---->YY

(3)

1) Many-to-one column---->yy_id (see personal habits)

2) column property in key-----> Many-to-one in One-to-many

3) Many-to-many Key----> This object _id (see personal habits)

Many-to-many in column---->yy_id

Relationship design of Java EE User, Department, role, authority entity and Hibernate mapping Profile Relationship Summary

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.