不一定要用 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;
-- 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;
}
}
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 >
<! 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 >
<! 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 >
< mapping resource ="com/demo/model/SubClass.hbm.xml" ></ mapping >
大功告成~~~~ :)