在这里Computer的主键由Student的主键决定 Student.hbm.xml: <class name="com.domain.Student" table="student" catalog="onetoone"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="name" not-null="true" /> </property> <!-- class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的 <one-to-one cascade="delete,save-update" name="computer" class="com.domain.Computer" property-ref="student"></one-to-one> --> <one-to-one name="computer"/> </class> Comuter.hbm.xml: <class name="com.domain.Computer" table="computer" catalog="onetoone"> <id name="id" type="java.lang.Integer"> <column name="id" /> <!--generator class="native" /--> <!--Computer的主键由Student的主键决定,可以看成是外键--> <generator class="foreign"> <param name="property">student</param> </generator> </id> <property name="name" type="java.lang.String"> <column name="name" not-null="true" /> </property> <!-- many开头的是代表该表持有外键 <many-to-one name="student" class="com.domain.Student" unique="true"> <column name="student_id" /> </many-to-one> --> <one-to-one name="student"/> </class>