Component映射(值对象映射)
在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,
component可以称为是值对象(DDD)
采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高
<!--
User: Comtact:
private int id; private String email;
private String name; private String address;
private Comtact comtact; private String phone;
-->
< class name ="User" table ="t_user" >
< id name ="id" >
< generator class ="native" />
</ id >
< property name ="name" />
< component name ="comtact" >
< property name ="email" />
< property name ="address" />
< property name ="phone" />
</ component >
</ class >
User: Comtact:
private int id; private String email;
private String name; private String address;
private Comtact comtact; private String phone;
-->
< class name ="User" table ="t_user" >
< id name ="id" >
< generator class ="native" />
</ id >
< property name ="name" />
< component name ="comtact" >
< property name ="email" />
< property name ="address" />
< property name ="phone" />
</ component >
</ class >
复合(联合)主键映射
通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法
<
class
name
="com.bjsxt.hibernate.FiscalYearPeriod"
table
="t_fiscal_year_period"
>
< composite-id name ="fiscalYearPeriodPK" >
< key-property name ="fiscalYear" />
< key-property name ="fiscalPeriod" />
</ composite-id >
< property name ="beginDate" />
< property name ="endDate" />
< property name ="periodSts" />
</ class >
< composite-id name ="fiscalYearPeriodPK" >
< key-property name ="fiscalYear" />
< key-property name ="fiscalPeriod" />
</ composite-id >
< property name ="beginDate" />
< property name ="endDate" />
< property name ="periodSts" />
</ class >
public
class
FiscalYearPeriodPK
implements
Serializable
{
//核算年
private int fiscalYear;
//核算月
private int fiscalPeriod;
public int getFiscalYear() {
return fiscalYear;
}
public void setFiscalYear(int fiscalYear) {
this.fiscalYear = fiscalYear;
}
public int getFiscalPeriod() {
return fiscalPeriod;
}
public void setFiscalPeriod(int fiscalPeriod) {
this.fiscalPeriod = fiscalPeriod;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + fiscalPeriod;
result = prime * result + fiscalYear;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
if (fiscalPeriod != other.fiscalPeriod)
return false;
if (fiscalYear != other.fiscalYear)
return false;
return true;
}
}
public class FiscalYearPeriod {
private FiscalYearPeriodPK fiscalYearPeriodPK;
//开始日期
private Date beginDate;
//结束日期
private Date endDate;
//状态
private String periodSts;
}
//核算年
private int fiscalYear;
//核算月
private int fiscalPeriod;
public int getFiscalYear() {
return fiscalYear;
}
public void setFiscalYear(int fiscalYear) {
this.fiscalYear = fiscalYear;
}
public int getFiscalPeriod() {
return fiscalPeriod;
}
public void setFiscalPeriod(int fiscalPeriod) {
this.fiscalPeriod = fiscalPeriod;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + fiscalPeriod;
result = prime * result + fiscalYear;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
if (fiscalPeriod != other.fiscalPeriod)
return false;
if (fiscalYear != other.fiscalYear)
return false;
return true;
}
}
public class FiscalYearPeriod {
private FiscalYearPeriodPK fiscalYearPeriodPK;
//开始日期
private Date beginDate;
//结束日期
private Date endDate;
//状态
private String periodSts;
}