自己创建 hibernate mapping

不一定要用 MyEclipse 的 Hibernate 框架生成数据库表的映射文件,其实很简单:
 
在 Eclipse 环境中,新建一个项目并添加 Hibernate 框架.
 
假设有两个表,分别是 MainClass 和 SubClass ,下面是两个表的表结构:(使用 MySQL 数据库)

Create   table  MainClass(
-- ID
MID  int  AUTO_INCREMENT  primary   key ,
-- main class name
MClsName  varchar ( 20 not   null )ENGINE = MyISAM  DEFAULT  CHARSET = utf8;

Create   table  SubClass(
-- sub class ID
SID  int  AUTO_INCREMENT  primary   key ,
-- sub class name
SClsName  varchar ( 20 not   null ,
-- main class ID (外键)
MID  int   not   null )ENGINE = MyISAM  DEFAULT  CHARSET = utf8;

现在来创建 MainClass 的类 MainClass.java,代码如下:

 

public   class  MainClass  ... {
    
private   int  mainClsId;
    
private  String mainClsName;

    
//  mainClsId Getter and Setter
     public   int  getMainClsId()  ... {
        
return  mainClsId;
    }

    
public   void  setMainClsId( int  mainClsId)  ... {
        
this .mainClsId  =  mainClsId;
    }


    
//  mainClsName Getter and Setter
     public  String getMainClsName()  ... {
        
return  mainClsId;
    }

    
public   void  setMainClsName(String mainClsName)  ... {
        
this .mainClsName  =  mainClsName;
    }

}



// SubClass.java,代码如下:
public   class  SubClass  ... {
    
private   int  subClsId;
    
private  String subClsName;
    
private   int  mainClsId;
    
private  MainClass mainClass;
    
    
//  mainClass Getter and Setter
     public  MainClass getMainClass()  ... {
        
return  mainClass;
    }

    
public   void  setMainClass(MainClass mainClass)  ... {
        
this .mainClass  =  mainClass;
    }

    
//  mainClsId Getter and Setter
     public   int  getMainClsId()  ... {
        
return  mainClsId;
    }

    
public   void  setMainClsId( int  mainClsId)  ... {
        
this .mainClsId  =  mainClsId;
    }

    
    
//  subClsId Getter and Setter
     public   int  getSubClsId()  ... {
        
return  subClsId;
    }

    
public   void  setSubClsId( int  subClsId)  ... {
        
this .subClsId  =  subClsId;
    }

    
    
//  subClsName Getter and Setter
     public  String getSubClsName()  ... {
        
return  subClsName;
    }

    
public   void  setSubClsName(String subClsName)  ... {
        
this .subClsName  =  subClsName;
    }

    
}

接着就是写 映射文件了,
MainClass.java 的映射文件 MainClass.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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
    
< class  name ="com.demo.model.MainClass"  table ="MainClass"  catalog ="test" >
        
< id  name ="mainClsId"  type ="integer" > <!-- name 是 MainClass.java 中的 mainClsId 属性 -->
            
< column  name ="MID"   /> <!-- name 是 MainClass 表中对应的列名 -->
            
< generator  class ="native"   />
        
</ id >
        
< property  name ="mainClsName"  type ="string" >
            
< column  name ="MClsName"  length ="20"  not-null ="true"   />
        
</ property >
    
</ class >
</ hibernate-mapping >

SubClass.java 的映射文件 SubClass.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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
    
< class  name ="com.demo.model.SubClass"  table ="SubClass"  catalog ="test" >
        
< id  name ="subClsId"  type ="integer" > <!-- name 是 SubClass.java 中的 subClsId 属性 -->
            
< column  name ="SID"   /> <!-- name 是 SubClass 表中对应的列名 -->
            
< generator  class ="native"   />
        
</ id >
        
< property  name ="subClsName"  type ="string" >
            
< column  name ="SClsName"  length ="20"  not-null ="true"   />
        
</ property >
        
<!-- 这里很重要 ,作用是声明这段是数据库表中的外键 -->
        
< property  name ="mainClsId"  column ="MID"   />
        
<!-- 这里的 column ,应该对应声明 外键的 name ,即 mainClsId ,而 name="mainClass" 就是 SubClass 里的一个属性 -->
                
< many-to-one  name ="mainClass"  column ="mainClsId"  
                        class
="com.demo.model.MainClass"  
                        lazy
="false"  
                        not-found
="ignore"  
                        cascade
="none"  
                        insert
="false"  
                        update
="false"   />
    
</ class >
</ hibernate-mapping >

好..最后一步就是在 hibenate 的配置文件中加上这两个 映射文件 地址就可以了

 

     < mapping  resource ="com/demo/model/MainClass.hbm.xml" ></ mapping >
    
< mapping  resource ="com/demo/model/SubClass.hbm.xml" ></ mapping >

 

大功告成~~~~ :)

 

来源:http://www.fish888.com/hibernate-mapping-t174284

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值