Hibernate技术应用随笔--关联关系建立

  前些时候写了篇Hibernate的基本原理与技术实现,现在就来说说hibernate里头一些常用的技术应用,欢迎吐槽!

一、对象映射,针对一个实体,通过hibernate 建立与关系数据库的映射关联,有两种方式。

1)、一个实体Article.java ,可以建立一个单独映射文件 Article.hbm.xml,Hibernate的基本原理一文中已有列子,可自行查看。

    package com.hibernate.test;  
    //读取 *.cfg.xml文件生成数据库表
    import org.hibernate.cfg.Configuration;  
    import org.hibernate.tool.hbm2ddl.SchemaExport;  
       
    public class ExportDB{    
        public static void main(String[]args){  
            //默认读取hibernate.cfg.xml文件  
            Configuration cfg = new Configuration().configure();  
            //根据读取到的配置文件在数据库中完成建表  
            SchemaExport export = new SchemaExport(cfg);  
            export.create(true, true);  
        }  
    }  

2)、通过给给实体添加注解的方式,达到建立映射的目的。

package com.hibernate.test;  
//根据实体类中的注解配置,生成数据库表
import org.hibernate.cfg.AnnotationConfiguration;  
import org.hibernate.cfg.Configuration;  
import org.hibernate.tool.hbm2ddl.SchemaExport;  
   
public class ExportDB{    
    public static void main(String[]args){  
        //默认读取hibernate.cfg.xml文件  
        Configuration cfg = new AnnotationConfiguration().configure();  
        SchemaExport export = new SchemaExport(cfg);  
        export.create(true, true);  
    }  
}
注意:hibernate 4.0 之后AnnotationConfiguration给丢弃了,增加了 ServiceRegistry接口;

Hibernate4.1已经可以自动建表,所以开发时只需要在实体中配置好注解就OK。不需要考虑怎么建表。不需要提前建立 ExportDB 工具去提起建表。

Configuration cfg = new Configuration();
ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.configure().buildSessionFactory(serviceRegistry);
配置注解的实体:详细的注解使用,就得各位看官仔细去看相关文档了

/**
 * 系统用户扩展信息
 *
 * $Autuor$
 */
@Entity
@Table(name = "ERP_SYSTEM_ACCOUNT_EXTENSION")//注解数据库中的表名
public class AccountExtension {

    /**
     * 数据权限级别.
     */
    private Integer dataLevel;


    @Column(name = "F_DATA_LEVEL")//注解对应数据库中的字段名
    public Integer getDataLevel() {
        return this.dataLevel;
    }

    public void setDataLevel(final Integer dataLevel) {
        this.dataLevel = dataLevel;
    }
}

二、对象关联映射

1)、一对多,多对一 : CommodityInfo( 一对多 ) CommoditySubInfo,实现方式分两种(实体注解,配置 *.hbm.xm 文件l)

——————————————————————————————————

CommodityInfo实体中的注解:主键CommodityInfo_id

  @OneToMany(mappedBy = "commodityInfo")
  public List<CommoditySubInfo> getCommoditySubInfo() {
    return commoditySubInfo;
  }
  public void setCommoditySubInfo (List<CommoditySubInfo> commoditySubInfo) {
    this.commoditySubInfo = commoditySubInfo;
  }
CommoditySubInfo实体中的注解:主键 CommoditySubInfo_id

  @ManyToOne
  @JoinColumn(name = "F_COMMODITY_INFO" )
  public CommodityInfo getCommodityInfo() {
    return commodityInfo;
  }
  public void setCommodityInfo(CommodityInfo commodityInfo) {
    this.commodityInfo = commodityInfo;
  }

在注解中 mappedBy = "commodityInfo" 表示所在该表为从表,关系的维护在 commodityInfo元素所在的 CommoditySubInfo 表;

在数据库中的sql 语句体现:在CommoditySubInfo 表中建立了一个外键 F_COMMODITY_INFO,用于关联到 CommodityInfo表的主键 CommodityInfo_id

————————————————————————————————

CommodityInfo.hbm.xml 文件

<set name="commoditySubInfo" inverse="true" cascade="all">
     <key column="CommoditySubInfo_id"></key>
     <one-to-many class="com.bean.CommoditySubInfo"></one-to-many>
</set>

CommoditySubInfo.hbm.cml 文件

<many-to-one name="commodityInfo" class="com.bean.CommodityInfo" 
column="CommodityInfo_id" not-null="true" cascade="all"></many-to-one>
在配置中:name表示在该表中的元素


以上两种方式都可以建立一对多的关系


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值