hibernate中映射关系总结

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[]

转载于:https://www.cnblogs.com/lt123/p/7244916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值