Hibernate映射關係(一)

本次測試的條件:
eclipse3.2+MyEclipse5.0+Sql server 2K
第一、在sql中建立兩個表student,與course
student::字段爲stuId,stuName,courseId
course:字段爲courseId,courseName
兩個表的DDL如下很簡單:
create table "tytc"."dbo"."student"(
        "stuId" 
int not null,
       "stuName" 
char(10null,
       "courseId" 
int not null,
        
constraint "PK_student" primary key ("stuId")
    )

create table "tytc"."dbo"."course"(
        "courseId" 
int not null,
       "courseName" 
char(10null,
        
constraint "PK_course" primary key ("courseId")
    )
建立兩表之間的關係:student中的courseId與course中的courseId
第二、利用MyEclipse中的工具自動生成映射文件,如下
Student.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="lcy.bo.Student" table="student" schema="dbo" catalog="tytc">
        
<id name="stuId" type="java.lang.Integer">
            
<column name="stuId" />
            
<generator class="assigned" />
        
</id>
        
<many-to-one name="course" class="lcy.bo.Course" fetch="select">
            
<column name="courseId" not-null="true" />
        
</many-to-one>
        
<property name="stuName" type="java.lang.String">
            
<column name="stuName" length="10" />
        
</property>
    
</class>
</hibernate-mapping>

Course.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="lcy.bo.Course" table="course" schema="dbo" catalog="tytc">
        
<id name="courseId" type="java.lang.Integer">
            
<column name="courseId" />
            
<generator class="assigned" />
        
</id>
        
<property name="courseName" type="java.lang.String">
            
<column name="courseName" length="10" />
        
</property>
        
<set name="students" inverse="true">
            
<key>
                
<column name="courseId" not-null="true" />
            
</key>
            
<one-to-many class="lcy.bo.Student" />
        
</set>
    
</class>
</hibernate-mapping>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章