Hibernate entity mapping file Many-to-many relationships simple application skills

Source: Internet
Author: User

The first step is to write a comment:
<!--xx attribute, this class and yy (Class) of the many-to-one
<!--xx attribute, this class is one-to-many with yy (class)
<!--xx attribute, many-to-many for this class and yy (Class)---
<!--xx attribute, one-to-one with YY (class)


Part Two, copy template
<!--xx attribute, this class and yy (Class) of the many-to-one
<many-to-one name= "" class= "" column= "" ></many-to-one>
<!--xx attribute, this class is one-to-many with yy (class)
<set name= "" >
<key column= "" ></key>
<one-to-many class= ""/>
</set>
<!--xx attribute, many-to-many for this class and yy (Class)---
<set name= "" table= "" >
<key column= "" ></key>
<many-to-many class= "" column= "" ></many-to-many>
</set>
<!--xx attribute, one-to-one with YY (class)
<many-to-one name= "" class= "" column= "" unique= "true" ></many-to-one>


The third step, fill in the blanks:
<!--xx attribute, this class and yy (Class) of the many-to-one
<many-to-one name= "xx" class= "Yy" column= "" ></many-to-one>
<!--xx attribute, this class is one-to-many with yy (class)
<set name= "XX" >
<key column= "" ></key>
<one-to-many class= "Yy"/>
</set>
<!--xx attribute, many-to-many for this class and yy (Class)---
<set name= "xx" table= "" >
<key column= "" ></key>
<many-to-many class= "Yy" column= "" ></many-to-many>
</set>
<!--xx attribute, one-to-one with YY (class)

<many-to-one name= "xx" class= "Yy" column= "" unique= "true" ></many-to-one>

Description
Many-to-one, a-to-many column's name is taken by itself, but the corresponding two column of the two relations must be the same;
Generally in many parties, the attribute name +id as the column value, a pair of more than one side also use this column, so it is possible;


Many-to-many, tables is a multi-to-many intermediate table in general naming that includes two table names. The column in key is worth the set foreign key, referring to the foreign key referencing the current table, this object is +id. Many-to-many refers to the foreign key referring to each other, the General association object name +id;

Here's an example:

Entity class:
Role

/** * Entity: Post * @author Jelly *  */public class Role implements serializable{private Long id;private String name;private set<user> users = new hashset<user> ();

Hbn.xml

User:

/** * Entity: User * @author Jelly *  */public class User implements Serializable{private Long id;private Department departmen t;//Department Private set<role> roles = new hashset<role> ();}
User.hbm.xml


Department
/** * Entity: Department * @author Jelly */public class Department implements Serializable{private Long id;private set<user> User s = new hashset<user> ();p rivate Department parent;private set<department> children = new hashset< Department> ();}
Department.hbm.xml






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.