Hibernate映射关系的详细介绍
本次测试的条件:eclipse3.2+MyEclipse5.0+Sql server 2K
第一、在sql中建立两个表student,与course
student::字段为stuId,stuName,courseId
course:字段为courseId,courseName
两个表的DDL如下很简单:
createtable"tytc"."dbo"."student"(
"stuId"intnotnull,
"stuName"char(10)null,
"courseId"intnotnull,
constraint"PK_student"primarykey("stuId")
)
createtable"tytc"."dbo"."course"(
"courseId"intnotnull,
"courseName"char(10)null,
constraint"PK_course"primarykey("courseId")
)
"stuId"intnotnull,
"stuName"char(10)null,
"courseId"intnotnull,
constraint"PK_student"primarykey("stuId")
)
createtable"tytc"."dbo"."course"(
"courseId"intnotnull,
"courseName"char(10)null,
constraint"PK_course"primarykey("courseId")
)
第二、利用MyEclipse中的工具自动生成映射文件,如下
Student.hbm.xml为:
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipse-HibernateTools
-->
<hibernate-mapping>
<classname="lcy.bo.Student"table="student"schema="dbo"catalog="tytc">
<idname="stuId"type="java.lang.Integer">
<columnname="stuId"/>
<generatorclass="assigned"/>
</id>
<many-to-onename="course"class="lcy.bo.Course"fetch="select">
<columnname="courseId"not-null="true"/>
</many-to-one>
<propertyname="stuName"type="java.lang.String">
<columnname="stuName"length="10"/>
</property>
</class>
</hibernate-mapping>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipse-HibernateTools
-->
<hibernate-mapping>
<classname="lcy.bo.Student"table="student"schema="dbo"catalog="tytc">
<idname="stuId"type="java.lang.Integer">
<columnname="stuId"/>
<generatorclass="assigned"/>
</id>
<many-to-onename="course"class="lcy.bo.Course"fetch="select">
<columnname="courseId"not-null="true"/>
</many-to-one>
<propertyname="stuName"type="java.lang.String">
<columnname="stuName"length="10"/>
</property>
</class>
</hibernate-mapping>
Course.hbm.xml为:
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipse-HibernateTools
-->
<hibernate-mapping>
<classname="lcy.bo.Course"table="course"schema="dbo"catalog="tytc">
<idname="courseId"type="java.lang.Integer">
<columnname="courseId"/>
<generatorclass="assigned"/>
</id>
<propertyname="courseName"type="java.lang.String">
<columnname="courseName"length="10"/>
</property>
<setname="students"inverse="true">
<key>
<columnname="courseId"not-null="true"/>
</key>
<one-to-manyclass="lcy.bo.Student"/>
</set>
</class>
</hibernate-mapping>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipse-HibernateTools
-->
<hibernate-mapping>
<classname="lcy.bo.Course"table="course"schema="dbo"catalog="tytc">
<idname="courseId"type="java.lang.Integer">
<columnname="courseId"/>
<generatorclass="assigned"/>
</id>
<propertyname="courseName"type="java.lang.String">
<columnname="courseName"length="10"/>
</property>
<setname="students"inverse="true">
<key>
<columnname="courseId"not-null="true"/>
</key>
<one-to-manyclass="lcy.bo.Student"/>
</set>
</class>
</hibernate-mapping>
本文地址:http://www.45fan.com/dnjc/73162.html