2012年6月27日
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="Card" table="tbl_card">
<id name="cardId" column="pk_card_id">
<generator class="native"/>
</id>
<property name="cardNo" column="card_no"/>
<!-- 需要维护关系的属性,就不是普通属性 -->
<!-- Person和Card沟通的桥梁是外键fk_card_id -->
<one-to-one name="person" property-ref="card"/>
<!-- property-ref="card":通过Person的外键pk_card_id,可以得到pk_person_id -->
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="Person" table="tbl_person">
<id name="personId" column="pk_person_id">
<generator class="native"/>
</id>
<property name="personName" column="person_name"/>
<!-- 唯一外键约束 -->
<!-- 此一对一属于多对一的特例 -->
<!-- 先假设多个人共用一张卡,再给fk_card_id添加唯一约束,就变成了一对一关系 -->
<many-to-one name="card" column="fk_card_id" unique="true" cascade="all"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="Clazz" table="tbl_class">
<id name="classId" column="pk_class_id">
<generator class="native"/>
</id>
<property name="className" column="class_name"/>
<!-- inverse="false":不放弃维护关系的权利,由Clazz维护关系 -->
<!-- inverse="true":放弃维护关系的权利,由Student维护关系 -->
<set name="students" inverse="true" cascade="all">
<!-- 一的一方为多的一方指定外键 -->
<key column="fk_class_id"/>
<!-- 让Clazz认识认识students是什么 -->
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="Student" table="tbl_student">
<id name="studentId" column="pk_student_id">
<generator class="native"/>
</id>
<property name="studentName" column="student_name"/>
<many-to-one name="clazz" column="fk_class_id" cascade="all"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="Role" table="tbl_role">
<id name="roleId" column="pk_role_id">
<generator class="native"/>
</id>
<property name="roleName" column="role_name"/>
<!-- 对于多对多的关系,需要一张中间表 -->
<set name="users" table="tbl_user_role">
<!-- 中间表,通过什么字段,跟Role表产生关系 -->
<key column="fk_role_id"/>
<!-- 让Role认识User --><!-- 中间表,通过什么字段,跟User表产生关系 -->
<many-to-many class="User" column="fk_user_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.hibernate.pojo">
<class name="User" table="tbl_user">
<id name="userId" column="pk_user_id">
<generator class="native"/>
</id>
<property name="userName" column="uesr_name"/>
<set name="roles" table="tbl_user_role">
<key column="fk_user_id"/>
<many-to-many class="Role" column="fk_role_id"/>
</set>
</class>
</hibernate-mapping>
public class Export{
private void mian(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}