首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP > 自己创建 hibernate mapping
【标  题】:自己创建 hibernate mapping
【关键字】:hibernate,mapping
【来  源】:http://blog.csdn.net/Eddie_Lau/archive/2007/04/19/1570126.aspx

自己创建 hibernate mapping

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

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

Create table SubClass(
--sub class ID
SID int AUTO_INCREMENT primary key,
--sub class name
SClsName varchar(20not 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>

 

大功告成~~~~ :)

input的CSS样式,分别对应不同的type的写法:【上一篇】
实现双击网页后页面自动向上滚动:【下一篇】
【相关文章】
  • hibernate3的一个bug
  • 关于spring+hibernate中的单元测试问题
  • 在weblogic中使用spring查找hibernate映射文件的陷阱
  • Hibernate建模工具CowNewStudio1.0.3发布
  • hibernate 配置文件之hibernate.cfg.xml说明
  • hibernate的表结构以及表之间的关系的 *.hbm.xml
  • 一个Hibernate 的面试题, 现场分析问题
  • PowerDesigner中设置Hibernate一对多,多对一属性
  • JSF+Hibernate+Spring学习
  • 总结hibernate三中关联关系
  • 【随机文章】
  • 我自己对Linux源代码的注释 (转)
  • Maven、Hibernate升级小贴士
  • Ulead SmartSaver Pro 3.0秘籍点点通(二十一)
  • 文件及目录的写权限
  • Photoshop绘制三菱标志(2)
  • ASP中Cookie读写的实现方法
  • 利用 typename 消除歧义
  • 专业音乐享受:玩转Foobar2000
  • 网络命令一览表(绝对实用)
  • IM Smarter小介绍
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.