部门和员工属于一对多的关系

员工的账户属于一对一关系

账户和权限属于多对多关系

department.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.demo.first.domain.Department" table="department">
 3         <id name="did" column="did">
 4             <generator class="native"/>
 5         </id>
 6         <property name="name" column="name"/>
 7         <property name="description" column="description"/>
 8         <set name="employeeSet" inverse="true" cascade="save-update">
 9             <!-- 外键的名称(字段名):也就是说如果是一对多的关系,那么会在多的一方生成的外键名称是 column 中的值,
10                 这个值是可以改变的,建议和对应的名称一致 -->
11             <key column="wj_id"></key>
12             <one-to-many class="com.demo.first.domain.Employee"/>
13         </set>
14     </class>
15 </hibernate-mapping>

employee.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.demo.first.domain.Employee" table="employee">
 3         <id name="eid" column="eid">
 4             <generator class="foreign"><!-- 直接将主键和外键进行关联,主键同时作为外键使用,在 hibernate 中 one-to-one 没办法在代码中体现出新字段名,可以使用many-to-one来生成一个字段,也可以使用此方法来生成外键 -->
 5                 <param name="property">userAccount</param>
 6             </generator>
 7         </id>
 8         <property name="name" column="name" length="20"/>
 9         <property name="gender" column="gender"/>
10         <property name="birthday" column="birthday"/>
11         <property name="description" column="description"/>
12         <one-to-one name="userAccount" class="com.demo.first.domain.UserAccount" constrained="true" cascade="save-update"/>
13         <!-- 和 department 中配置 column 的名称是一样的,需要对应 -->
14         <many-to-one name="department" class="com.demo.first.domain.Department" column="wj_id"/>
15     </class>
16 </hibernate-mapping>

privilege.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.demo.first.domain.Privilege" table="privilege">
 3         <id name="pid" column="pid">
 4             <generator class="native"/>
 5         </id>
 6         <property name="action"/>
 7         <set name="userAccountSet" table="userAccount_privilege">
 8             <!-- 和一对多一样的操作,column 是在生成的字段名(外键),是可以改变的,多对多的关系中,会生成一个中间表,那么在
 9                 column 中配置的参数就是生成的中间表中的字段名,另外一方配置的字段名需相同-->
10             <key column="wj_pid"></key>
11             <many-to-many class="com.demo.first.domain.UserAccount" column="ua_wj_id"/>
12         </set>
13     </class>
14 </hibernate-mapping>

useraccount.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.demo.first.domain.UserAccount" table="userAccount">
 3         <id name="uaid" column="uaid">
 4             <generator class="native"/>
 5         </id>
 6         <property name="loginName" column="loginName"/>
 7         <property name="password" column="password"/>
 8         <one-to-one name="employee" class="com.demo.first.domain.Employee"/>
 9         <set name="privilegeSet" table="userAccount_privilege">
10             <!-- 对应于另一方的参数配置,column 的参数值和另一方相同,也就是生成的中间表的字段名相同 -->
11             <key column="ua_wj_id"></key>
12             <many-to-many column="wj_pid" class="com.demo.first.domain.Privilege"/>
13         </set>
14     </class>
15 </hibernate-mapping>

另附上可以打开 hibernate debug 模式(可以查看所有的操作,包括自动建表语句等等):

需要的 jar 包等:

hibernate文件下载:https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.4.2.Final/hibernate-release-5.4.2.Final.zip/download

在 hibernate-release-5.4.2.Final\project\etc 中找到 log4j.properties 这个文件,直接放到 src 的根目录下,即可:

重点就是上面的那个语句,去掉注释(默认是打开的,没有注释)重启即可,

 

 2019-04-25