hibernate 映射组成关系
来源:互联网 发布:免费手机音量增大软件 编辑:程序博客网 时间:2024/06/05 05:08
建立域模型和关系数据模型有着不同的出发点:
- 域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程
- 在没有数据冗余的情况下, 应该尽可能减少表的数目, 简化表之间的参照关系, 以便提高数据的访问速度
Hibernate 把持久化类的属性分为两种:
Pay.java
- 值(value)类型: 没有 OID, 不能被单独持久化, 生命周期依赖于所属的持久化类的对象的生命周期
- 实体(entity)类型: 有 OID, 可以被单独持久化, 有独立的生命周期(如果实体类型包含值类型,这个值类型就是一个组件,虽然是由两个类,不同在数据库中用同一张数据库表表示)
显然无法直接用 property 映射 pay 属性
Hibernate 使用 <component> 元素来映射组成关系, 该元素表名 pay 属性是 Worker 类一个组成部分, 在 Hibernate 中称之为组件
component
Worker.java
package com.atguigu.hibernate.entities;public class Worker {private Integer id;private String name;private Pay pay;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;}public Pay getPay() {return pay;}public void setPay(Pay pay) {this.pay = pay;}}Worker.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2014-1-2 16:14:33 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="com.atguigu.hibernate.entities"> <class name="Worker" table="WORKER"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <!-- 映射组成关系 --> <component name="pay" class="Pay"> <!-- 指定组成关系的组件的属性 --> <property name="monthlyPay" column="MONTHLY_PAY"></property> <property name="yearPay" column="YEAR_PAY"></property> <property name="vocationWithPay" column="VOCATION_WITH_PAY"></property> </component> </class></hibernate-mapping>
package com.atguigu.hibernate.entities;public class Pay {private int monthlyPay;private int yearPay;private int vocationWithPay;public int getMonthlyPay() {return monthlyPay;}public void setMonthlyPay(int monthlyPay) {this.monthlyPay = monthlyPay;}public int getYearPay() {return yearPay;}public void setYearPay(int yearPay) {this.yearPay = yearPay;}public int getVocationWithPay() {return vocationWithPay;}public void setVocationWithPay(int vocationWithPay) {this.vocationWithPay = vocationWithPay;}}
parent属性:
这里在组件中引用了父类private Worker worker;
Pay.java
package com.atguigu.hibernate.entities;public class Pay {private int monthlyPay;private int yearPay;private int vocationWithPay;private Worker worker;public Worker getWorker() {return worker;}public void setWorker(Worker worker) {this.worker = worker;}public int getMonthlyPay() {return monthlyPay;}public void setMonthlyPay(int monthlyPay) {this.monthlyPay = monthlyPay;}public int getYearPay() {return yearPay;}public void setYearPay(int yearPay) {this.yearPay = yearPay;}public int getVocationWithPay() {return vocationWithPay;}public void setVocationWithPay(int vocationWithPay) {this.vocationWithPay = vocationWithPay;}}Worker.java
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2014-1-2 16:14:33 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="com.atguigu.hibernate.entities"> <class name="Worker" table="WORKER"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <!-- 映射组成关系 --> <component name="pay" class="Pay"> <parent name="worker"/> <!-- 指定组成关系的组件的属性 --> <property name="monthlyPay" column="MONTHLY_PAY"></property> <property name="yearPay" column="YEAR_PAY"></property> <property name="vocationWithPay" column="VOCATION_WITH_PAY"></property> </component> </class></hibernate-mapping>
0 0
- Hibernate映射组成关系
- Hibernate 映射组成关系
- Hibernate映射组成关系
- hibernate映射组成关系
- Hibernate -- 映射组成关系
- hibernate 映射组成关系
- Hibernate 映射组成关系
- Hibernate映射组成关系
- Hibernate之映射组成关系
- Hibernate映射组成关系简析
- hibernate 映射组成关系(第六章)
- 精通Hibernate——映射组成关系
- hibernate映射 【映射组成关系】,初学例子五
- 精通HIBERNATE---------读书笔记第十章 映射组成关系
- Hibernate学习笔记(五)【映射组成关系】
- Hibernate学习笔记(五)【映射组成关系】
- 7.映射组成关系
- 1009--映射组成关系
- [Android 4.4.2] 泛泰A870 Mokee4.4.2 20140531 RC1.0 by syhost
- Arduino 笔记
- More Effective C++笔记
- wince 6.0 开机自启动
- Apache 配置二级域名匹配目录及多个站点配置
- hibernate 映射组成关系
- codeforce 6C
- Struts2拦截器
- 写给过去
- linux下MySQL安装及设置
- 软件开发之业务逻辑
- NanShan 的一段最简单的 CFile 读写文件源码
- Office问题整理连载
- Linux shell脚本中判断参数用法