I. Overview of many-to-many relationships
Take the relationship between the student and the curriculum as an example.
1. There are three tables involved in a many-to-many relationship, two entity tables, and a table dedicated to maintaining relationships.
2. The two entity classes in a many-to-many relationship should add the properties of each other's set collection separately and provide the set and get methods.
3. It is more complex to configure a mapping file than a one-to-many relationship.
Second, the mapping file
<Setname= "Students"Table= "Course_stu"Cascade= "Save-update"Inverse= "true"> <!--now the inverse property is set to true so that the relationship between course and Stu cannot be maintained through the course configuration file. - <Key> <columnname= "CID"></column> </Key> <Many-to-manyclass= "Com.kdyzm.moretomore.domain.Stu"> <columnname= "Sid"></column> </Many-to-many> </Set>
Many-to-many relational mapping files are written to explain:
Set Label:
Name property: property names for set collections in persisted classes
Table property: Tables Name of the intermediate table
Cascade Property: An important property that transforms the state of a cascading object from other states to a persisted state without throwing an exception.
Inverse property: Configures whether the relationship is maintained, if true, the relationship is not maintained, and if it is false, the relationship is maintained.
Key tag: Sets the foreign key, which is the foreign key in the intermediate table Course_stu table, indicating that the current table can be found through this field through the Course_stu table.
Many-to-many Tags: Setting association tables
Class Property: Sets the persisted class corresponding to the associated table
Column property: Sets the foreign key for the associated table that corresponds to the intermediate table.
"Java EE Learning Day 46th" "Hibernate learning the third day" "Many-to-many relationship mapping"