第十章 基于Annotation的关系映射 多对一与一对多

简介: <p style="color:rgb(54,46,43); font-family:Arial; line-height:26px"><span style="font-size:14px">如果下面部分内容有不明白的可以查找:</span></p> <p style="color:rgb(54,46,43); font-family:Arial; line-height:26px">

如果下面部分内容有不明白的可以查找:

基于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;
	}
}




目录
相关文章
|
网络协议 Linux
nmcli命令详解
【4月更文挑战第9天】`nmcli`是Red Hat 7及CentOS 7后的网络管理命令,用于配置网卡并持久化设置。它可以显示网络连接信息(如`connection show`、`dev status`),控制网卡状态(启用、停用、删除连接),以及修改配置(如IP地址、DNS)。其他功能包括检查NetworkManager状态、开关网络连接和查看系统网络状态。要了解全部详情和高级用法,建议查阅相关文档。
1413 1
|
12月前
|
运维 Ubuntu Linux
定时任务管理详解:cron与at的配置与使用
定时任务管理详解:cron与at的配置与使用
760 2
|
9月前
|
开发工具 git
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
733 3
git 使用之remote: File [4e21e71a555febaa4dfaaa05cf7eeb606ea96ae2] size 104.090MB, exceeds quota 100MB remote: Please remove the file[s] from history and try again 报错如何解决-优雅草卓伊凡
|
12月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
1012 4
|
移动开发 JavaScript 前端开发
如何使用 JavaScript 进行跨域请求?
如何使用 JavaScript 进行跨域请求?
|
Java 关系型数据库 MySQL
基于SpringBoot+Vue企业级工位管理系统(源码+部署说明+演示视频+源码介绍+lw)(1)
基于SpringBoot+Vue企业级工位管理系统(源码+部署说明+演示视频+源码介绍+lw)
325 0
|
JSON Java 数据格式
controller方法的返回值
controller方法的返回值
|
API
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
566 0
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
|
存储 Prometheus 监控
在Ubuntu系统上安装与配置Prometheus的步骤
通过以上步骤,您应该已经成功在Ubuntu系统上安装并配置了Prometheus。您现在可以开始使用Prometheus收集和分析您的系统和应用程序的指标数据了。
897 1
|
数据库 数据安全/隐私保护 数据库管理
基于SpringBoot+Vue企业级工位管理系统(源码+部署说明+演示视频+源码介绍+lw)(2)
基于SpringBoot+Vue企业级工位管理系统(源码+部署说明+演示视频+源码介绍+lw)
202 0