映射学习笔记

1.N-1映射示例:

假设有两张表,建立主外键关联

 

员工<--->部门

员工 Domain:

private Integer id;	
private String name;
private Department dept;

员工Hibernate配置:

<class name="Student" table="student_db">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native">
				<param name="native">id_inc</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="60"></column>
		</property>
		<!-- conlumn对应外键,name为对象名称 -->
		<many-to-one name="dept">
			<column name="name_"></column>
		</many-to-one>
	</class>

部门Domain:

private Integer id;
private String name;

 部门hbm

	<class name="Department" table="department_db">
		<id name="id" type="java.lang.Integer">
			<column name="id"/>
			<generator class="native">
				<param name="native">id_inc</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="60" ></column>
		</property>
	</class>
Department dept1 = new Department();
Department dept2 = new Department();
dept1.setName("财务");
dept2.setName("后勤");
Student st1 = new Student();
Student st2 = new Student();
st1.setName("风姿");
st2.setName("导师");
st1.setDept(dept1);
st2.setDept(dept1);
session.save(dept1);
session.save(dept2);
session.save(st1);
session.save(st2);
trans.commit();

2.one-to-many映射

当需要查询出一个部门中有多少个学生的情况下需要配置:

Department:
private Set<Student> stu;
	public Set<Student> getStu() {
		return stu;
	}
	public void setStu(Set<Student> stu) {
		this.stu = stu;
	}

 相应的hbm.xml文件为:

<!-- 配置onetomany,cascade为级联,要保存一个部门的时候同时加载两个学生 -->
<set name="stu" >
	<key column="dept_id"/>
	<one-to-many class="Student"/>
</set>

测试:

 

1.
//查询一个部门中有多少名学生
Department dept1 = (Department) session.get(Department.class, 3);
Set<Student> set = dept1.getStu();
for(Student s:set){
	System.out.println("student:"+s.getName());
}
2.
Department department=new Department();
department.setName("业务部门");

Student stu1=new Student();
stu1.setName("顺平");
Student stu2=new Student();
stu2.setName("小明");

Set sets=new HashSet<Student>();
sets.add(stu1);
sets.add(stu2);
department.setStu(sets);
session.save(stu1);
session.save(stu2);
session.save(department);

 其中在测试的过程中一直出现一个错误:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource 

后来发现hibernate.config.xml中配置了:

 

<mapping resource="many2one/Student.hbm.xml" />
<mapping resource="many2one/Department.hbm.xml" />
<mapping resource="one2many/Student.hbm.xml" />
<mapping resource="one2many/Department.hbm.xml" />

 可能是名称相同互相干扰的原因,去掉不相干的一组后可以正常测试!

3.one-to-one主键一对一关联

案例:一个人对应一个身份证

 

private Integer id;
private String name;
private Idcard idcard;

 

 

private Integer id;
private Date date;
private Person person;

 

 采用手动分配主键策略

 

<class name="Person" table="person_db">
	<id name="id" type="java.lang.Integer">
		<column name="id"/>
		<generator class="assigned">
			<param name="assigned">id_inc</param>
		</generator>
	</id>
	<property name="name" type="java.lang.String">
		<column name="name" length="60" ></column>
	</property>
	<one-to-one name="idcard"></one-to-one>
</class>

 设置外键,从而是Person中的id与Idcard中id保持一致

 

class name="Idcard" table="idcard_db">
	<id name="id" type="java.lang.Integer">
		<generator class="foreign">
			<param name="property">person</param>
		</generator>
	</id>
	<property name="date" type="java.util.Date">
		<column name="date" not-null="true"></column>
	</property>
	<one-to-one name="person"/>
</class>

 测试:

 

        Person p1=new Person();
	p1.setId(1);
	p1.setName("jk");
	Idcard idCard=new Idcard();
	idCard.setDate(new Date());
	idCard.setPerson(p1);//表示idCard对象是属于p1这个对象.
	session.save(p1);//先保存人
	session.save(idCard);

4.one-to-one基于外键

这样设置的话,那么idcard_db表中就会多一个person的id列

 

<class name="Idcard" table="idcard_db">
	<id name="id" type="java.lang.Integer">
		<generator class="assigned">
		</generator>
	</id>
	<property name="date" type="java.util.Date">
		<column name="date" not-null="true"></column>
	</property>
	<many-to-one name="person" unique="true"/>
</class>

 测试:

 

			Person p1=new Person();
			p1.setId(1);
			p1.setName("jk");
			Idcard idCard=new Idcard();
			idCard.setId(1234);
			idCard.setDate(new Date());
			idCard.setPerson(p1);//表示idCard对象是属于p1这个对象.
			session.save(p1);//先保存人
			session.save(idCard);

5.many-to-many

这个映射关系一般很难表示,可以拆分为多对一或者一对多,一对一的组合

例如学生与课程之间的关系为多对多关系

可以建立一个学生课程表作为学生表与课程表的中间表

那么这样,学生与学生课程表之间就是一对多关系,课程与学生课程表之间也是一对多关系。

学生Domain设计:

 

	private Integer id;
	private String name;
	private Set<StuCourse> stuCourse;

学生hbm配置,因为有一个一对多关系,所以要把Set映射到hbm文件中:

 

	<class name="Student" table="student_db">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native">
				<param name="native">id_inc</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" not-null="true"></column>
		</property>
		<set name="stuCourse">
			<!-- 用于指定外键 -->
			<key column="student_id"></key>
			<one-to-many class="StuCourse" />
		</set>
	</class>
课程Domain,与学生Domain类似:
	private Integer id;
	private String name;
	private Set<StuCourse> stuCourse;
对应的hbm文件:
	<class name="Course" table="course_db">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native">
				<param name="native">id_inc</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" not-null="true"></column>
		</property>
		<set name="stuCourse">
			<!-- 用于指定外键 -->
			<key column="course_id"></key>
			<one-to-many class="StuCourse" />
		</set>
	</class>
 学生课程表:
	private Integer id;
	private Student stu;
	private Course course;
	private Integer grade;
 该表与学生表和课程表关系均为多对一关系,所以配置文件为:
	<class name="StuCourse" table="stucourse_db">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native">
				<param name="native">id_inc</param>
			</generator>
		</id>
		<property name="grade" type="java.lang.Integer">
			<column name="grade" not-null="true"></column>
		</property>
		<many-to-one name="stu" column="student_id" />
		<many-to-one name="course" column="course_id" />
	</class>
 注意的是,学生课程表中many-to-one中的column作为学生和课程表中外键来使用。
 

 

你可能感兴趣的:(one-to-many,many-to-one,one-to-one,hibernate映射,many-to-many)