閱讀430 返回首頁    go 阿裏雲 go 技術社區[雲棲]


第九章 關係映射 一對多關聯映射

如: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 />
		</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 ></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、多對一與一對多可以一起用,形成雙向關係。

      多對一映射的使用:https://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)



最後更新:2017-04-03 18:52:09

  上一篇:go C++筆試題匯總(45題)
  下一篇:go Quartus中仿真時出現no simulation input file assignment specify