建立域模型和关系数据模型有着不同的出发点:
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>
hibernate 映射组成关系,布布扣,bubuko.com
原文:http://blog.csdn.net/qilixiang012/article/details/27838677