第十章 基于Annotation的关系映射 多对一与一对多
来源:互联网 发布:电商部美工岗位说明书 编辑:程序博客网 时间:2024/06/05 08:19
如果下面部分内容有不明白的可以查找:
基于Annotation的关系映射 前期准备:http://blog.csdn.net/p_3er/article/details/9061911
基于xml的多对一:http://blog.csdn.net/p_3er/article/details/9036759
基于xml的一对多:http://blog.csdn.net/p_3er/article/details/9036921
本文是把多对一与一对多结合起来了,形成一个双向的映射。如果只想要单向的话,把别外一边的注解去掉就是了。
Department:
@Entity@Table(name = "department", catalog = "hibernate")public class Department implements java.io.Serializable {private Integer id;private String name;private Set<Employee> employees = new HashSet<Employee>(0);public Department() {}public Department(String name, Set<Employee> employees) {this.name = name;this.employees = employees;}@Id@GeneratedValue@Column(name = "id", unique = true, nullable = false)public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}@Column(name = "name", nullable = false, length = 45)public String getName() {return this.name;}public void setName(String name) {this.name = name;}@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "department")/*一对多。department和employee的一对多关系中,当指定department中的mappedBy后,关系只能被employee来主动维护.也就是employee级联的处理department. 之前的映射文件: <set name="employees" inverse="false" cascade="all"><key column="department_id"></key><one-to-many class="cn.framelife.hibernate.entity.Employee"/></set>*/public Set<Employee> getEmployees() {return this.employees;}public void setEmployees(Set<Employee> employees) {this.employees = employees;}}
Employee:
@Entity@Table(name = "employee", catalog = "hibernate")public class Employee implements java.io.Serializable {private Integer id;private Department department;private String name;public Employee() {}@Id@GeneratedValue@Column(name = "id", unique = true, nullable = false)public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}@ManyToOne(fetch = FetchType.LAZY)@JoinColumn(name = "department_id")/*多对一。 <many-to-one name="department" column="department_id"></many-to-one>*/public Department getDepartment() {return this.department;}public void setDepartment(Department department) {this.department = department;}@Column(name = "name", nullable = false, length = 45)public String getName() {return this.name;}public void setName(String name) {this.name = name;}}
- 第十章 基于Annotation的关系映射 多对一与一对多
- 第十章 基于Annotation的关系映射 多对多
- Hibernate的关系映射-------多对一与一对多
- Hibernate学习——(八)基于Annotation的多对一、一对多映射
- Hibernate关联关系配置-----基于连接表的双向一对多/多对一映射配置
- hibernate的一对多和多对一关系映射
- Hibernate中一对多、多对一关系的映射
- Hibernate的关系映射-------多对一与一对多的实现举例
- 第十章 基于Annotation的关系映射 前期准备
- 第十章 基于Annotation的关系映射 一对一
- MyBatis框架基于Annotation注解的一对多关联映射
- 【Hibernate】的一对多与多对一映射
- hibernate一对一,一对多,多对一,多对多关系映射与级联
- Hibernate 基于注解的方式 实现的关系映射案例(一对一关系,一对多关系,多对多关系)
- Hibernate实体关系映射——双边的多对一、一对多关系
- Hibernate关联关系映射 一对一 一对多 多对一
- 关系映射一对多,多对一详解
- Hibernate 关系映射--多对一、一对多总结篇
- python cherrypy RESTful API,cherrypy支持跨域ajax访问,CROS,crossdomain ajax
- VS2008连接Sqlserver2008 - ADO方式
- magento 开发必备知识集锦
- python rename and sort file or directry
- linux jdk安装步骤
- 第十章 基于Annotation的关系映射 多对一与一对多
- 关于Eclipse+tomcat环境配置
- 动态设置select默认值代码
- 在WCF使用SOAP1.1
- ViewPage判断左右滑动
- 图片的加载效果(水填充)
- 基于Predictive Parsing的ABNF语法分析器(六)——AbnfParser文法解析器之多个符号连接的情形(如rule和CRLF)
- 写的第一个长sql,纪念下
- 【Linux基础】为什么要学shell