---
title: "自己创建 hibernate mapping"
author: "cai555"
date: "2010-09-16T22:30:51Z"
tags: []
categories: ["企业架构"]
slug: "hibernate-mapping"
draft: false
lastmod: "2010-09-16T22:30:51Z"
id: 765280
url: "https://cai555.iteye.com/blog/765280"
---

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

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Create   table  MainClass(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) -- ID  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) MID  int  AUTO\_INCREMENT  primary   key ,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) -- main class name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) MClsName  varchar ( 20 )  not   null )ENGINE = MyISAM  DEFAULT  CHARSET = utf8;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) Create   table  SubClass(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) -- sub class ID  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) SID  int  AUTO\_INCREMENT  primary   key ,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) -- sub class name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) SClsName  varchar ( 20 )  not   null ,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) -- main class ID (外键)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) MID  int   not   null )ENGINE = MyISAM  DEFAULT  CHARSET = utf8;

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

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public   class  MainClass  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private   int  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  String mainClsName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  mainClsId Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   int  getMainClsId()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setMainClsId( int  mainClsId)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .mainClsId  =  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  mainClsName Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public  String getMainClsName()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setMainClsName(String mainClsName)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .mainClsName  =  mainClsName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) // SubClass.java,代码如下:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) public   class  SubClass  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private   int  subClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  String subClsName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private   int  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  MainClass mainClass;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  mainClass Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public  MainClass getMainClass()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  mainClass;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setMainClass(MainClass mainClass)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .mainClass  =  mainClass;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  mainClsId Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   int  getMainClsId()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setMainClsId( int  mainClsId)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .mainClsId  =  mainClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  subClsId Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   int  getSubClsId()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  subClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setSubClsId( int  subClsId)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .subClsId  =  subClsId;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      //  subClsName Getter and Setter  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public  String getSubClsName()  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return  subClsName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public   void  setSubClsName(String subClsName)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this .subClsName  =  subClsName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }

接着就是写 映射文件了,  
MainClass.java 的映射文件 MainClass.hbm.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;? xml version="1.0" ?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;!--    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     Mapping file autogenerated by MyEclipse - Hibernate Tools  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt; hibernate-mapping &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt; class  name ="com.demo.model.MainClass"  table ="MainClass"  catalog ="test" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt; id  name ="mainClsId"  type ="integer" &gt; &lt;!-- name 是 MainClass.java 中的 mainClsId 属性 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; column  name ="MID"   /&gt; &lt;!-- name 是 MainClass 表中对应的列名 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; generator  class ="native"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;/ id &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt; property  name ="mainClsName"  type ="string" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; column  name ="MClsName"  length ="20"  not-null ="true"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;/ property &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;/ class &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/ hibernate-mapping &gt;

SubClass.java 的映射文件 SubClass.hbm.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;? xml version="1.0" ?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;!--    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     Mapping file autogenerated by MyEclipse - Hibernate Tools  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt; hibernate-mapping &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt; class  name ="com.demo.model.SubClass"  table ="SubClass"  catalog ="test" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt; id  name ="subClsId"  type ="integer" &gt; &lt;!-- name 是 SubClass.java 中的 subClsId 属性 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; column  name ="SID"   /&gt; &lt;!-- name 是 SubClass 表中对应的列名 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; generator  class ="native"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;/ id &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt; property  name ="subClsName"  type ="string" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              &lt; column  name ="SClsName"  length ="20"  not-null ="true"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;/ property &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;!-- 这里很重要 ,作用是声明这段是数据库表中的外键 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt; property  name ="mainClsId"  column ="MID"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)          &lt;!-- 这里的 column ,应该对应声明 外键的 name ,即 mainClsId ,而 name="mainClass" 就是 SubClass 里的一个属性 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                  &lt; many-to-one  name ="mainClass"  column ="mainClsId"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         class ="com.demo.model.MainClass"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         lazy ="false"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         not-found ="ignore"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         cascade ="none"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         insert ="false"    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                         update ="false"   /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;/ class &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/ hibernate-mapping &gt;

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

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt; mapping  resource ="com/demo/model/MainClass.hbm.xml" &gt;&lt;/ mapping &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt; mapping  resource ="com/demo/model/SubClass.hbm.xml" &gt;&lt;/ mapping &gt;

 

大功告成~~~~ :)

 

来源：[http://www.fish888.com/hibernate-mapping-t174284](http://www.fish888.com/hibernate-mapping-t174284)