Hibernate关联关系映射-----单向多对一映射配置
来源:互联网 发布:生产者消费者模式 php 编辑:程序博客网 时间:2024/04/27 11:17
在这里举了一个不太恰当的例子:双亲和孩子。当然举这个例子也有一定的道理,一个孩子至少有两个parent,但是例子只是例子,重点不是例子而是配置方法。下面我们看一下配置的详细步骤:
实体:
package uni.many2one;public class Child {private int id;private String name;public Child(int id, String name) {super();this.id = id;this.name = name;}public Child() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}package uni.many2one;public class Parent {private int id;private String name;private Child child;public Parent() {}public Parent(int id, String name, Child child) {super();this.id = id;this.name = name;this.child = child;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Child getChild() {return child;}public void setChild(Child child) {this.child = child;}}
hbm配置文件:
<hibernate-mapping><class name="uni.many2one.Child"><id name="id" column="did"><generator class="native" /></id><property name="name" type="string" column="name"></property></class></hibernate-mapping><hibernate-mapping><!-- <class name="org.hibernate.wk.Student" dynamic-insert="true" dynamic-update="true"> --><class name="uni.many2one.Parent"><id name="id" column="did"><generator class="native" /></id><property name="name" type="string" column="name"></property><!-- configure the many to one association --><many-to-one name="child" column="child"></many-to-one></class></hibernate-mapping>
测试文件:
public void testAdd() {SessionFactory sf = HibernateUtil.getSessionFactory();Session session = sf.getCurrentSession();session.beginTransaction();Child c1 = new Child();c1.setName("child1");Parent mother = new Parent();mother.setName("Mother");mother.setChild(c1);Parent father = new Parent();father.setName("Daddy");father.setChild(c1);session.save(c1);session.save(mother);session.save(father);session.beginTransaction().commit();}
测试结果:
Hibernate: insert into Child (name) values (?)Hibernate: insert into Parent (name, child) values (?, ?)Hibernate: insert into Parent (name, child) values (?, ?)这里我没有贴出来表的结构,因为表hibernate会自动生成的,多以就没有浪费地方。
hibernate关联关系映射的配置中务必要搞清楚谁是主动配置方,谁是被配置的,如果搞不清楚很容易在实体操作的时候出现错误。尤其是在保存实体的时候,很容易出现引用为空的情况。
- Hibernate关联关系映射-----单向多对一映射配置
- Hibernate关系映射(7)_多对一单向关联
- hibernate-关系映射(多对一单向关联)-8
- Hibernate多对一单向关联映射
- hibernate:多对一单向关联映射
- Hibernate多对一映射单向关联
- Hibernate多对一关联映射(单向)
- 【HIbernate框架学习】:Hibernate对象关系映射之单向多对一关联映射
- hibernate映射总结详解: 映射多对一单向关联关系
- 【SSH系列】Hibernate映射-- 多对一单向关联映射
- Hibernate映射—— 多对一单向关联映射
- Hibernate关联关系映射-----基于连接表的单向多对多映射配置
- Hibernate关联关系映射(单向一对多、单向多对一、双向一对多)
- Hibernate关联关系映射(多对一关联映射)
- hibernate映射关系 一对多、多对一单向关联 一对多双向关联
- Hibernate关系映射(10)_多对多单向关联
- Hibernate关联映射之多对一单向关联映射
- Hibernate关联关系映射-----单向一对多配置
- c++ 原来我一直没写对双目运算符函数
- 编程与生活
- sphinx 运行出现 error while loading shared libraries: libmysqlclient.so.16: cannot open shared object
- POJ 1273
- 2011.7.22
- Hibernate关联关系映射-----单向多对一映射配置
- 如何发表源代码帖
- 解决不能下载Xcode的办法
- TAOWEN的闲言碎语___Python完全新手教程
- 一个值得纪念的日子
- 24岁的程序员
- Data-Intensive Text Processing with MapReduce第三章(6)-MapReduce算法设计-3.5相关连接(RELATIONAL JOINS)
- ANNOUNCEMENT: Updated Program License Agreement解决办法
- windows下Android开发环境搭建