1.一对多
父表 name,column,class顺序
<set name="employees">
<key column="departmentId"></key>
<one-to-many class="Employee"/>
</set>
子表 (外键列)
<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
2.一对一(基于外键的方式)
(外键表 子表 )
<many-to-one name="employee" class="Employee" column="employeeId" unique="true"></many-to-one>
<one-to-one name="userAccount" class="UserAccount" property-ref="employee"></one-to-one>
3.多对多
Privilege.hbm.xml文件夹下
<set name="userAccounts" table="privilege_account" >
<key column="privilegeId"></key>
<many-to-many class="UserAccount" column="userAccountId"></many-to-many>
</set>
UserAccount.hbm.xml文件夹下
<set name="privileges" table="privilege_account" >
<key column="userAccountId"></key>
<many-to-many class="Privilege" column="privilegeId"></many-to-many>
</set>
需注意的地方
1.<property></property>标签中name属性必填 column,type可填,可不填
2.javabean中只能含有类,集合,不能含有外键
private Long id;
private String name;
private String description;
private Department parent;
private Set<Department> children=new HashSet<Department>();
3.
几条查询语句
String hql="from Department d where d.parent is null order by id desc";
hql="select e.department.id,count(*) as num from Employee e group by e.department.id";
String dml="update Employee e set e.department=? where id=?";//注意更新 不能修改任何一个表的自动生成主键
String hql="select u.employee.id from UserAccount u where u.employee.id is not null";查询语句 ,注意
from Employee 返回的是javabean
select id from employee where xx=xx 有可能是obj 也有可能是ob[]