Nhibernate教程2(2)
3. 创建.Net类和与其对应的XML文件
(1) 单表的情况
单表的情况就是指该表没有和其他表有关联的情况。这样情况下,可以用软件来辅助产生类和对应的XML文件。在这里,仍然采用手写的方式来生成这两个文件。对于课程表,需要写的类和XML如下所示:
类文件(Course.cs)
using System;
namespace NHibernateTest{
public class Course{
public Course(){}
private int courseId;
private string courseName;
private DateTime dateCreated;
public int CourseId{
get { return courseId; }
set { courseId = value; }
}
public string CourseName{
get { return courseName; }
set { courseName = value; }
}
public DateTime DateCreated{
get { return dateCreated; }
set { dateCreated = value; }
}
}
}
|
XML文件(Course.hbm.xml)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Course, NHibernateTest" table="Course">
<id name="CourseId" column="CourseId" type="Int32">
<generator class="native" />
</id>
<property name="CourseName" column= "CourseName" type="string" length="40"/>
<property name="DateCreated" type="DateTime" length="8"/>
</class>
</hibernate-mapping>
|