hibernate基础-Component+[联合]映象

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 >

复合(联合)主键映射

通常将复合主键相关的属性,单独放到一个类中
 * 此类必须实现序列化接口
 * 覆写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 >

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值