NHibernate 基本使用(一对一、组件、一对多)

一、基类定义
public abstract class Root
{
   public string OBID {get; set;}
   public DateTime LastUpdate {get; set;}
}

二、一对一处理(User & Account):
class define:
public class User : Root
{
   public string Name {get; set;}
   public string Password {get; set;}
   public Account Account {get; set;}
}
public class Account : Root
{
   public User User {get; set;}
   public decimal Balance {get; set;}
}

mapping define:
<class name="User" table="MD_Users">
  <id name="OBID" unsaved-value="null" length="32">
    <generator class="uuid.hex" />
  </id>
  <timestamp name="LastUpdate"/>
  <property name="Name" unique="true" not-null="true" />
  <property name="Password" not-null="true" />
  <one-to-one name="Account" class="Account" property-ref="User" />
</class>
<class name="Account" table="MD_Accounts">
  <id name="OBID" unsaved-value="null" length="32">
    <generator class="uuid.hex" />
  </id>
  <timestamp name="LastUpdate"/>
  <property name="Balance" not-null="true" />
  <many-to-one name="User" class="User" column="UsrOBID" unique="true" />
</class>

samples:
NHibernate.ISession s = getSession();
NHibernate.ITransaction t = s.BeginTransaction();
User user = new User();
user.Name = "name";
user.Password = "***";
Account acnt = new Account();
acnt.Balance = 1999M;
acnt.User = user;
user.Account = acnt;

try
{
  s.SaveOrUpdate(user);
  s.SaveOrUpdate(acnt);
  t.Commit();
}
catch
{
  t.Rollback();
}
finally
{
  s.Close();
}

三、组件Component
class define:
public class Person : Root
{
  public Name Name {get; set;}
  public DateTime Brithday {get; set;}
}
public class Name
{
  public string FirstName {get; set;}
  public string MiddleName {get; set;}
  public string LastName {get; set;}
}

mapping define:
<class name="Person" table="MD_Persons">
  <id name="OBID" unsaved-value="null" length="32">
    <generator class="uuid.hex" />
  </id>
  <timestamp name="LastUpdate"/>
  <property name="Brithday" />
  <component name="Name" class="Name">
    <property name="FirstName" />
    <property name="MiddleName" />
    <property name="LastName" />
    </component>
</class>

ddl export:
CREATE TABLE [dbo].[MD_Persons] (
 [OBID] [nvarchar] (32) NOT NULL ,
 [LastUpdate] [datetime] NOT NULL ,
 [Brithday] [datetime] NULL ,
 [FirstName] [nvarchar] (255) NULL ,
 [MiddleName] [nvarchar] (255) NULL ,
 [LastName] [nvarchar] (255) NULL ,
 [CreationDate] [datetime] NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[MD_Persons] WITH NOCHECK ADD
  PRIMARY KEY  CLUSTERED
(
 [OBID]
)  ON [PRIMARY]

四、一对多处理(Media & MediaFile):
class define:
public class Media : Root
{
  public string Name {get; set;}
  public IList MediaFiles {get; set;}
}
public class MediaFile : Root
{
  public Media Media {get; set;}
  public string Name {get; set;}
  public string Path {get; set;}
}

mapping define:
<class name="Media" table="MD_Medias">
  <id name="OBID" unsaved-value="null" length="32">
    <generator class="uuid.hex" />
  </id>
  <timestamp name="LastUpdate"/>
  <property name="Name" unique="true" not-null="true" />
  <bag name="MediaFiles">
    <key column="MdOBID" />
    <one-to-many class="MediaFile"/>
  </bag>
</class>
<class name="MediaFile" table="MD_MediaFiles">
  <id name="OBID" unsaved-value="null" length="32">
    <generator class="uuid.hex" />
  </id>
  <timestamp name="LastUpdate"/>
  <property name="Name" unique="true" not-null="true" />
  <property name="Path" />
  <many-to-one name="Media" class="Media" column="MdOBID" />
</class>

samples:
Media med = new Media();
med.Name = "The 5th Element";

MediaFile mf1 = new MediaFile();
mf1.Name = "CD1";
mf1.Path = "D:\movies\The5thElement\CD1.rmvb";
mf1.Media = med;
med.MediaFiles.Add(mf1);

MediaFile mf2 = new MediaFile();
mf2.Name = "CD2";
mf2.Path = "D:\movies\The5thElement\CD2.rmvb";
mf2.Media = med;
med.MediaFiles.Add(mf2);

try
{
  s.SaveOrUpdate(mf1);
  s.SaveOrUpdate(mf2);
  s.SaveOrUpdate(med);
  t.Commit();
}
catch
{
  t.Rollback();
}
finally
{
  s.Close();
}

你可能感兴趣的:(Hibernate)