hibernate 映射组成关系
建立域模型和关系数据模型有着不同的出发点:
- 域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程
- 在没有数据冗余的情况下, 应该尽可能减少表的数目, 简化表之间的参照关系, 以便提高数据的访问速度
- 值(value)类型: 没有 OID, 不能被单独持久化, 生命周期依赖于所属的持久化类的对象的生命周期
- 实体(entity)类型: 有 OID, 可以被单独持久化, 有独立的生命周期(如果实体类型包含值类型,这个值类型就是一个组件,虽然是由两个类,不同在数据库中用同一张数据库表表示)
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; } }
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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。