NHibernate中的many-to-many关系示范(转)

NHibernate中的many-to-many关系示范

1. 生成数据库
CODE:

use master
go
create database Hibernate
go
use Hibernate
go

create table Users (user_id int identity primary key, name varchar(100))
go

create table Groups(group_id int identity primary key, name varchar(100), description varchar(100))
go

create table UserGroups (group_id int foreign key references Groups(group_id),
user_id int foreign key references Users(user_id))
go



2. User.cs

CODE:

using System;
using System.Collections;

public class User {

  public User() {
  }

  public int UserId
  {
    get { return userId; }
    set { userId = value; }
  }

  public string Name
  {
    get { return name; }
    set { name = value; }
  }

  public IList Groups
  {
    get { return groups; }
    set { groups = value; }
  }

  private int userId;
  private string name;
  private IList groups = new ArrayList();

} //class User



3. Group.cs

CODE:

using System;
using System.Collections;

public class Group {

  public Group() {
  }

  public int GroupId
  {
    get { return groupId; }
    set { groupId = value; }
  }

  public string Name
  {
    get { return name; }
    set { name = value; }
  }

  public string Description
  {
    get { return description; }
    set { description = value; }
  }

  public IList Users
  {
    get { return users; }
    set { users = value; }
  }

  private int groupId;
  private string name;
  private string description;
  private IList users = new ArrayList();

} //class Group


4. User.hbm.xml

CODE:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="User, UserGroup" table="users">
<id name="UserId" column="user_id" type="Int32"

unsaved-value="0">
  <generator class="identity" />
</id>
<property name="Name" column= "name" type="String"/>
<bag name="Groups" table="UserGroups" inverse="true">
     <key column="user_id" />
   <many-to-many column="group_id" class="Group,

UserGroup" />
</bag>
</class>
</hibernate-mapping>



5. Group.hbm.xml

CODE:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Group, UserGroup" table="groups">
<id name="GroupId" column="group_id" type="Int32"

unsaved-value="0">
  <generator class="identity" />
</id>
<property name="Name" column= "name" type="String"/>
<property name="Description" column= "description"

type="String"/>
<bag name="Users" table="UserGroups">
     <key column="group_id" />
   <many-to-many column="user_id" class="User, UserGroup"

/>
</bag>
</class>
</hibernate-mapping>



6. TestUser.cs

CODE:

using System;
using NHibernate;
using NHibernate.Cfg;
using System.Text;
using System.Collections;
using NHibernate.Expression;

public class TestCreate
{
   ISession session;

   public TestCreate()
   {

   Configuration cfg = new Configuration();

   cfg.AddXmlFile("User.hbm.xml");
   cfg.AddXmlFile("Group.hbm.xml");

   ISessionFactory factory = cfg.BuildSessionFactory();
   session = factory.OpenSession();
   }

public void Test()
{
  User user1 = new User();
  user1.Name = "test1";
  User user2 = new User();
  user2.Name = "test2";

  Group group1 = new Group();
  group1.Name = "group1";
  Group group2 = new Group();
  group2.Name = "group2";

  user1.Groups.Add( group2);
  user2.Groups.Add( group1 );
  group1.Users.Add( user2 );
  group2.Users.Add( user1);

  ITransaction trans = null;
  try {
    trans = session.BeginTransaction();

    session.Save( user1 );
    session.Save( user2 );
    session.Save( group1 );
    session.Save( group2 );

    trans.Commit();
  }
  catch ( Exception e ) {
    if ( trans != null ) trans.Rollback();
    throw e;
  }
  finally {
    session.Close();
  }

}

   static void Main()
   {
       TestCreate tc = new TestCreate();
       tc.Test();
   }

}


7. TestUser.exe.config

CODE:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
      <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,

Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<nhibernate>
<add
  key="hibernate.connection.provider"      
  value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
  key="hibernate.dialect"              
  value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
  key="hibernate.connection.driver_class"      
  value="NHibernate.Driver.SqlClientDriver"
/>
<add
  key="hibernate.connection.connection_string"
  value="Server=localhost;initial catalog=hibernate;Integrated Security=SSPI"
/>
<add key="hibernate.show_sql" value="true" />
</nhibernate>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
  <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] -%m%n" />
  </layout>
  <root>
  <level value="INFO" />
  <appender-ref ref="ConsoleAppender" />
  </root>
</appender>
</log4net>
</configuration>


8. UserGroup.dll

csc /t:library /out:UserGroup.dll User.cs Group.cs

9. TestUser.exe

csc /r:Nhibernate.dll,UserGroup.dll TestUser.cs

10. runtime assemblies needed

HashCodeProvider.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll

posted @ 2008-09-03 17:13  YFeng_Lee  阅读(476)  评论(0编辑  收藏  举报