hibernate多对多关联(六)
来源:互联网 发布:网络签约作家 编辑:程序博客网 时间:2024/05/21 17:46
多对多关联关系配置
User和Role多对多关联关系,单向。User可以访问role。
publicclassUserimplements Serializable {
privateIntegerid;
privateStringname;
privateIntegerage;
private Set<Role>roles;
publicclassRole {
privateintrid;
privateStringinfo;
<classname="org.hibernate.tutorial.domain.User"table="user">
<idname="id"type="java.lang.Integer"column="id">
<generatorclass="native"/>
<!-- <generator class="foreign" ><param name="property">card</param>
</generator>-->
</id>
<setname="roles"table="user_role">
<keycolumn="uid"></key>
<many-to-manyclass="org.hibernate.tutorial.domain.Role"column="rid"></many-to-many>
</set>
</class>
<classname="org.hibernate.tutorial.domain.Role"table="role">
<idname="rid"column="rid"type="integer">
<generatorclass="native"/>
</id>
<propertyname="info"column="info"type="string"/>
</class>
测试
User user = (User)session.load(User.class,new Integer(46));
// Useruser = new User();
// user.setAge(11);
// user.setName("young");
Roler =new Role();
r.setInfo("guest");
// Set<Role>s = new HashSet<Role>();
// user.setRoles(s);
user.getRoles().add(r);
session.save(r);
session.save(user);
sql语句
Hibernate: select user0_.id asid0_1_, user0_.name as name0_1_, user0_.age as age0_1_, card1_.id as id4_0_,card1_.info as info4_0_ from user user0_ left outer join card card1_ onuser0_.id=card1_.id where user0_.id=?
Hibernate: select roles0_.uid asuid1_, roles0_.rid as rid1_, role1_.rid as rid2_0_, role1_.info as info2_0_from user_role roles0_ left outer join role role1_ on roles0_.rid=role1_.ridwhere roles0_.uid=?
Hibernate: insert into role (info)values (?)
Hibernate: insert into user_role(uid, rid) values (?, ?)
多对多关联关系,双向配置
在上述基础上,在Role.hbm.xml中添加
<setname="users"table="user_role">
<keycolumn="rid"></key>
<many-to-manyclass="org.hibernate.tutorial.domain.User"column="uid"></many-to-many>
</set>
书上说多对多关联关系性能不佳,由于引入了中间表,一次都写需要反复次数的数据库查询,因此应该避免大量使用(或者避免使用)同时应该采取延迟加载,节约不必要的开销。
- hibernate多对多关联(六)
- Hibernate(六)——多对多关联映射
- Hibernate(六)——多对多关联映射
- 【Hibernate系列】(六):关联映射之一对多
- hibernate详解(六)---多对多关联映射
- Hibernate 简单使用(六)多对多关联映射(二)
- 一口一口吃掉Hibernate(六)——多对多关联映射
- Hibernate关系映射(六)多对多单向关联@ManyToMany Annotation方式
- Hibernate(六)实现一对多、多对一映射关联关系
- Hibernate 关联映射 之 多对多 关联(一)
- Hibernate 关联关系映射(多对多关联)
- Hibernate 关联映射 之 多对多 关联(一)
- 【Hibernate框架】关联映射(多对多关联映射)
- Hibernate关联映射-----多对多关联
- Hibernate关联关系映射(多对一关联映射)
- Hibernate关联关系之一对多关联
- Hibernate 多对多关联
- hibernate 多对多关联
- html5笔记
- ListView中getChildAt(index)的使用注意事项
- MySQL 存储过程中的循环
- mysql导入sql报错 Unknown command '\''.
- 自己编写jQuery插件
- hibernate多对多关联(六)
- Revit二次开发入门——创建外部命令
- 关于微信分享支付的一些问题
- UE4简单巡逻BehaviorTree的构建
- fir.im Weekly - 17 个提升 iOS 开发效率的必备工具
- 图像处理中的数学原理详解14——曲面积分
- dp与px的转换
- 常用界面交替动画样式以及View动态样式
- impdp卡住,遇到等待事件Streams AQ: enqueue blocked on low memory