1. For example, table class (classid, class_no, classname) and student (studentid, studentname, class_no ),
Classid and studentid are primary keys.
The two tables are one-to-many relationships, and the two tables must be associated through classno.
In general, the data is stored in the student table as a foreign key through classid.
2. The specific hibernate configuration file is as follows:
Class. HBM. xml:
<Property
Name = "classno"
Type = "Java. Lang. String"
Column = "class_no"
Length = "30"
/>
<! -- Associations -->
<Set name = "Students"
Lazy = "false"
Inverse = "true"
Cascade = "all-delete-Orphan"
>
<Key column = "class_no" property-ref = "classno"/>
<One-to-least
Class = "student"
/>
</Set>
Student. HBM. xml:
<Role-to-one
Name = "class"
Class = "class"
Not-null = "true"
Property-ref = "classno"
>
<Column name = "class_no"/>
</Role-to-one>
3. Notes:
The property-ref attribute is generally used to solve the problem of the one-to-one relationship between legacy databases.
Property-ref (optional) Name of the corresponding property in the class associated with the other key. If not specified, the primary key of the associated class is used.
Property-ref = whether it is the field name in the database table, but the attribute name in the defined Java class. Be sure to pay attention to it.