第九章 关系映射 多对一关联映射
来源:互联网 发布:网络压力测试工具 编辑:程序博客网 时间:2024/05/01 16:17
如:department与employee
employee中有一个department_id的外键
Department:
public class Department implements Serializable {private Integer id;private String name;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;private Department department;public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}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></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><many-to-one name="department" column="department_id"></many-to-one></class></hibernate-mapping>
增加:
transaction = session.beginTransaction();Department department = new Department();department.setName("bb");session.save(department);Employee employee = new Employee();employee.setDepartment(department);employee.setName("li");session.save(employee);transaction.commit();
查询:
查询employee的时候可以得到外键关联的department对象。
- 第九章 关系映射 多对一关联映射
- 第九章 关系映射 多对多关联映射
- Hibernate关联关系映射(多对一关联映射)
- 第九章 关系映射 组件关联映射
- 第九章 关系映射 集合关联映射
- 第九章 关系映射 继承关联映射
- 第九章 关系映射 一对多关联映射
- Hibernate关联关系映射-----单向多对一映射配置
- Hibernate之关联关系映射(一对多和多对一映射,多对多映射)
- Hibernate关联映射--多对一映射
- 多对一关联映射
- Hibernate关联关系映射 一对一 一对多 多对一
- Hibernate关联关系映射(一对多/多对一)
- 【Hibernate】--关联关系映射:一对多和多对一
- Hibernate关系映射(7)_多对一单向关联
- 关联映射的关系(多对一)
- Hibernate关联关系映射_单边多对一
- Hibernate关联关系映射_多对一双边
- hdu1162-Eddy's picture
- 配置ODBC数据源
- Oracle 10g 完全参考手册之DECODE实现交叉报表--数据行转换为报表的列
- selenium使用中遇到的问题
- 泊车
- 第九章 关系映射 多对一关联映射
- Service的启动
- NSdate 时差转换
- 在struts2中如何获得session id
- 点击背景 关闭键盘
- 编译Android 4.0.4(打包出问题)
- 单链表反转
- 写两个程序出现StackOverflowError和
- Qt实现窗口tabwidget的拖出双击弹回功能