第九章 关系映射 一对多关联映射

如:departmentemployee

employee中有一个department_id的外键

Department:

public class Department implements Serializable {
	private Integer id;
	private String name;
	private Set<Employee> employees;

	public Set<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}

	public Department() {
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}



Employee:

public class Employee implements Serializable {
	private Integer id;
	private String name;

	public Employee() {
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}


Department.hbm.xml:(与普通的映射文件一样)

<hibernate-mapping>
	<class name="cn.framelife.hibernate.entity.Department" table="department"
		catalog="hibernate">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="45" not-null="true" />
		</property>	
                <set name="employees" inverse="false" cascade="all">
			<key column="department_id"></key>
			<one-to-many class="cn.framelife.hibernate.entity.Employee"/>
		</set>
	</class>
</hibernate-mapping>


Employee.hbm.xml:

<hibernate-mapping>
	<class name="cn.framelife.hibernate.entity.Employee" table="employee"
		catalog="hibernate">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="45" not-null="true" />
		</property>	
	</class>
</hibernate-mapping>

增加:

transaction = session.beginTransaction();
			Department department = new Department();
			department.setName("zzza");
			Set<Employee> set = new HashSet<Employee>();
			for(int i=0; i < 5; i++){
				Employee employee = new Employee();
				employee.setName("zz"+i);
				set.add(employee);
			}
			department.setEmployees(set);
			session.save(department);
			transaction.commit();


查询:

查询department的时候可以得到外键关联的所有employee对象。



注意事项:

a、错误提示:Field'department_id' doesn't have a default value
   
数据表中把"department_id"设成可以为空的,但是Hibernate先执行的是:

因为hibernate执行的顺序是这样的:

Hibernate:insert into hibernate.department (name) values (?)

Hibernate:insert into hibernate.employee (name) values (?)

Hibernate:insert into hibernate.employee (name) values (?)

Hibernate:insert into hibernate.employee (name) values (?)

Hibernate:insert into hibernate.employee (name) values (?)

Hibernate:insert into hibernate.employee (name) values (?)

Hibernate:update hibernate.employee set department_id=? where id=?

Hibernate:update hibernate.employee set department_id=? where id=?

Hibernate:update hibernate.employee set department_id=? where id=?

Hibernate:update hibernate.employee set department_id=? where id=?

Hibernate:update hibernate.employee set department_id=? where id=?

department_id外键是作为一个后来才更新的存在。所有不能设置为非空的。


b
Department.hbm.xml中要设置cascade="all"(级联),或其它有效值,不然,在保存Department对象时,相关的Employee对象不会被保存。(none,all,save-update,delete..)


c
Department.hbm.xmlset标签的inverse属性不能设置为"true"放弃维护关联关系),inverse的默认值是"false",所以不加inverse也可以。看书上说:在一对多的关联关系实现中,最好设置inverse="true",将有助于性能的改善。所以一开始就用了inverse="true"UserCard对象都分别正确写入数据库了,但是就是departmentID字段没有被自动写入。


d、多对一与一对多可以一起用,形成双向关系。

      多对一映射的使用:http://blog.csdn.net/p_3er/article/details/9036759


e.one-to-manymany-to-onemany-to-many懒加载分析:

必须同时满足下面的两个条件时才能实现懒散加载:

1).lazy!=false(lazy缺省方式就!=falselazy=proxy)

2).fetch=select(fetch缺省方式即为select)



上一篇:第九章 关系映射 多对一关联映射


下一篇:解决Ajax全局变量赋值的问题