这个例子好像是IBM Dev上的Hibernate介绍,被人改编过了,我再改编一次,呵呵:P
重点是弄清Hibernate中两个表间一对一、一对多、多对多的关联,数据模式如图:
建表后插入些数据,如下:
select * from classes
+-----------+----------+
| ClassesId | name |
+-----------+----------+
| 200201 | pro j2ee |
+-----------+----------+
select * from students
+------------+----------+-----------+
| StudentId | name | ClassesId |
+------------+----------+-----------+
| s_20020001 | andjia | 200201 |
| s_20020002 | vincent | 200201 |
| s_20020003 | mor | 200201 |
+------------+----------+-----------+
select * from courses
+----------+---------------+
| CourseId | name |
+----------+---------------+
| c_001 | computer-java |
| c_002 | computer-c++ |
+----------+---------------+
select * from address
+------------+------+--------+-------+--------+
| AddressId | City | Street | State | Zip |
+------------+------+--------+-------+--------+
| s_20020001 | as | sd | zse | 123333 |
| s_20020002 | bj | hd | bj | 123456 |
| s_20020003 | bb | cc | fdse | 121333 |
+------------+------+--------+-------+--------+
select * from student_course_link
+----------+------------+
| CourseId | StudentId |
+----------+------------+
| c_001 | s_20020001 |
| c_001 | s_20020002 |
| c_002 | s_20020002 |
| c_002 | s_20020003 |
+----------+------------+
student与address之间是1对1
student与classes之间是n对1
student与courses之间是n对n
OK,我们来看hbm.xml的写法,先看最关键的student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="xxxxxxx.hibernate.Student"
table="Students"
dynamic-update="false"
>
<id name="id"
column="StudentId"
type="string"
unsaved-value="any"
>
<generator class="assigned"/>
</id>
<property
name="name"
type="string"
update="true"
insert="true"
column="Name"
/>
<set name="courses"
table="Student_Course_Link"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key column="StudentId"/>
<many-to-many class="xxxxxxx.hibernate.Course"
column="CourseId"
outer-join="auto"
/>
</set>
<many-to-one
name="classes"
class="xxxxxxx.hibernate.Classes"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="ClassesId"
/>
<one-to-one
name="address"
class="xxxxxxx.hibernate.Address"
cascade="none"
outer-join="auto"
constrained="false"
/>
</class>
</hibernate-mapping>
后面many-to-one和one-to-one比较简单,主要是many-to-many这块,必须制定关联键,而且在Student的Bean里,要设一个Set,而不是一个对象,如下
public class Student
{
/**属性,和students表中的字段对应**/
private String id;
private String name;
/**和其它类之间的映射关系**/
private Set courses;
private Classes classes;
private Address address;
而在其他3个类的映射文件中,Address和一般的Bean完全一样,Classes类有个Set的私有域,用来存放多个学生,同样指定关联键
<set
name="students"
……
>
<key column="ClassesId"/>
<one-to-many class="xxxxxxs.hibernate.Student"/>
</set>
Course类的映射文件中的many-to-many与Student中的一样,就相应的表名等等正好颠倒一下。
于是我们可以根据StudentID取得一个Student对象,其中包含了对应的Course等信息,如:
Student st=(Student)sess.load(Student.class,request.getParameter("studentId"));
Set courses=st.getCourses();
for(Iterator it=courses.iterator();it.hasNext();){
Course course=(Course)it.next();
……
}
- 大小: 9.4 KB
分享到:
相关推荐
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向...Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
hibernate 中间表 希望给予新手帮助
NULL 博文链接:https://lf6627926.iteye.com/blog/1176785
Hibernate多表关联配置及错误解决方法,及时更新,相关信息待下回分解!
Hibernate数据关联技术笔记Hibernate数据关联技术笔记Hibernate数据关联技术笔记Hibernate数据关联技术笔记Hibernate数据关联技术笔记
hibernate表与表的关联代码 很有多实例,对新手应该帮助不少
hibernate关联查询 实例源码 java web 如果不明白hibernate关联查询不明白的可以看看,保证马上学会,注释,数据库都有,很简单易学的源码,谢谢下载!
hibernate多表联合查询实例,很经典的查询语句和总结.
hibernate的表关联操作,通过映射关系操作实体类增删改查数据库-hibernate02
Hibernate4中映射关系图解。
hibernate表之间的关联关系在配置文件如何体现
本速查表没有打成CHM! Hibernate实体映射表实用性较强! 希望大家喜欢!
该资源包含了一些常用的hibernate关联映射实例。包括一对多,多对多,继承映射,等,是学习hibernate的必备品
详细讲解hibernate的关联关系,以及用实例讲解,非常详细
hibernate表之间3种双向关联映射
hibernate关联关系
NULL 博文链接:https://dreamzhong.iteye.com/blog/1181526
NULL 博文链接:https://paladin1988.iteye.com/blog/1627678