property-ref用于指定关联类的一个属性,这个属性将会和本外键相对应
-
Java code
- 1.如表Class(ClassID,Class_No,ClassName)与Student(StudentID,studentName,Class_No), 其中ClassID,studentID为主键 两个表是一对多的关系,而要求两个通过ClassNo来关联. 而一般的情况下是通过ClassID,放在student表中作为外键. 2.具体的Hibernate的配置文件如下: 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-many class="Student" /> </set> Student.hbm.xml: <many-to-one name="class" class="Class" not-null="true" property-ref="classNo" > <column name="Class_No" /> </many-to-one> 3.注意点: property-ref属性一般用来解决遗留数据库One To Many关系的问题 property-ref(可选)被关联到此外键的类中的对应属性的名字,若没指定,使用被关联类的主键. property-ref=是不是数据库表中的字段名,而是定义的java类中的属性性名,一定要注意.