NHibernate 多主键设置

数据库结构,3个字段,StudentName, ClassName, ClassDate,都为char类型,主键为StudenName + ClassName。

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="CompoTest.Classes, CompoTest" table="Classes">
         <composite-id class="CompoTest.ClassInfo, CompoTest" name = "MyClassInfo">
              <key-property name="StudentName" type="String(10)" column="StudentName" />
              <key-property name="ClassName" type="String(10)" column="ClassName" />
         </composite-id>
         <property name="ClassDate" type="String(8)" column="ClassDate" />
   </class>
 </hibernate-mapping>

类:

 public class  Classes
 {
  public Classes()
  {

  }

  private System.String _ClassDate;
  public System.String ClassDate
  {
    get { return _ClassDate; }
   set { _ClassDate = value; }
  }

  private ClassInfo classInfo;
  public ClassInfo MyClassInfo
  {
    get { return classInfo; }
   set { classInfo = value; }
  }

 }

 public class ClassInfo
 {
  public ClassInfo()
  {
  }

  private string studentName;
  public string StudentName
  {
   set { studentName = value; }
   get { return studentName; }
  }

  private string className;
  public string ClassName
  {
   set { className = value; }
   get { return className; }
  }
 }

写几个测试用例:

  [Test]
  public void TestAdd()
  {
   ClassInfo classInfo = new ClassInfo();
   classInfo.ClassName = "Chinese";
   classInfo.StudentName = "Tom";

   Classes classes = new Classes();
   classes.ClassDate = "20041003";
   classes.MyClassInfo = classInfo;
   

   Configuration cfg = new Configuration();
   cfg.AddXmlFile("Classes.hbm.xml");
   ISessionFactory factory = cfg.BuildSessionFactory();
   ISession session = factory.OpenSession();
   ITransaction transaction = session.BeginTransaction();
   
   
   session.Save(classes);
   transaction.Commit();
   session.Close();

  }


  [Test]
  public void TestUpdate()
  {
   ClassInfo classInfo = new ClassInfo();
   classInfo.ClassName = "Chinese";
   classInfo.StudentName = "Tom";
   
   Classes classes = new Classes();
   
   Configuration cfg = new Configuration();
   cfg.AddXmlFile("Classes.hbm.xml");
   ISessionFactory factory = cfg.BuildSessionFactory();
   ISession session = factory.OpenSession();
   ITransaction transaction = session.BeginTransaction();

   classes = (Classes)session.Load(typeof(Classes),classInfo);
   classes.ClassDate = "20041231";

   session.Save(classes);

   transaction.Commit();
   session.Close();
  }

  [Test]
  public void TestDelete()
  {
   ClassInfo classInfo = new ClassInfo();
   classInfo.ClassName = "Chinese";
   classInfo.StudentName = "Tom";
   
   Classes classes = new Classes();
   
   Configuration cfg = new Configuration();
   cfg.AddXmlFile("Classes.hbm.xml");
   ISessionFactory factory = cfg.BuildSessionFactory();
   ISession session = factory.OpenSession();
   ITransaction transaction = session.BeginTransaction();

   classes = (Classes)session.Load(typeof(Classes),classInfo);
   session.Delete(classes);

   transaction.Commit();
   session.Close();
  }

测试成功。。。。。。

你可能感兴趣的:(Hibernate)