`

自己创建 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

分享到:
评论

相关推荐

    Hibernate映射关系配置:XML方式和注解方式

    NULL 博文链接:https://yangjb.iteye.com/blog/1186724

    hibernate配置

    DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping package="持久化对象所在的包路径"&gt; ...

    深入浅出Hibernate中文版 part1

    第6章 Hibernate 实战——创建RedSaga论坛 6.1 目标 6.2 E-R建模还是对象建模 6.3 E-R建模及工具集 6.4 对象建模 6.5 项目的目录组织和基础设施 6.6 测试优先 6.7 容器,session与事务 6.8 ...

    Hibernate笔记 马士兵

    第7课 建立Annotation版本的HellWorld 9 第8课 什么是O/R Mapping 11 一、 定义: 11 二、 Hibernate的创始人: 11 三、 Hibernate做什么: 12 四、 Hibernate存在的原因: 12 五、 Hibernate的优缺点: 12 六、 ...

    Hibernate1

    4.创建hibernate配置文件(文件名为hibernate.cfg.xml,一般我们将其放在根目录下面) &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ...

    hibernate笔记

    5 建立hibernate 配置文件hibernate.cfg.xml 6 6 建立Student 类 6 7 建立Student 映射文件 Student.hbm.xml 6 8 将映射文件加入到hibernate.cfg.xml中 6 9 写测试类Main,在Main中对Student对象进行直接的存储测试 6...

    hibernate 教程

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns ...

    Hibernate+中文文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    hibernate总结

    &lt;/hibernate-mapping&gt; Hibernate映射多对一: public class Emp implements java.io.Serializable { private Integer empid; private Dept dept; private String empname; //getter/setter方法略 } ...

    深入浅出Hibernate中文版 part2

    第6章 Hibernate 实战——创建RedSaga论坛 6.1 目标 6.2 E-R建模还是对象建模 6.3 E-R建模及工具集 6.4 对象建模 6.5 项目的目录组织和基础设施 6.6 测试优先 6.7 容器,session与事务 6.8 ...

    hibernate3.2中文文档(chm格式)

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    HibernateAPI中文版.chm

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate中文详细学习文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate 中文 html 帮助文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    11.2.3 Action Mapping(映射) 11.2.4 ActionForm(表单控制器) 11.2.5 ActionErrors(错误处理) 11.2.6 DispatchAction(多动作控制器) 11.3 利用Struts实现用户登录的示例 11.6.1 编写实现登录的页面login.jsp...

    最全Hibernate 参考文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    Hibernate入门.docx

    Hibernate是一种ORM框架,全称为 Object_Relative DateBase-Mapping ,在Java对象与关系数据库之间 建立某种映射,以实现直接存取Java对象 ! 为什么要使用Hibernate? 既然Hibernate是关于Java对象和关系数据库...

    SpringMVC+Hibernate全注解整合

    对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 mvc:annotation-driven --&gt; &lt;!-- 扫描包 --&gt; &lt;context:annotation-config/&gt; *" /&gt; ...

    Hibernate教程

    6.1.2. hibernate-mapping 6.1.3. class 6.1.4. id 6.1.4.1. Generator 6.1.4.2. 高/低位算法(Hi/Lo Algorithm) 6.1.4.3. UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and ...

    hibernate 体系结构与配置 参考文档(html)

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

Global site tag (gtag.js) - Google Analytics