各位大仙们~谁来帮帮我啊~废话不多说。
我创建了一个组(Group)表,一个模块(Module)表,一个关系表:组模块(GroupModule)表。
Group:GroupID,GroupName,ParentID;
Module:ModuleID,ModuleName,ModuleLocation;
GroupModule:GMID,GroupID,ModuleID.
映射配置已经配好。
现在要对关系表进行插入和删除的操作。具体该怎么做啊?
我是这样做的:在NHibernateDAO里写了一个方法:
public void addGroupModule(Group g,Module m)
        {
            using (tra = session.BeginTransaction())
            {
                try
                {           
                    g.modules.Add(m);//这儿运行后直接跳到catch。
                    m.groups.Add(g);
                    session.Save(g);
                    session.Flush();
                    tra.Commit();
                }
                catch (Exception e)
                {
                    if (tra != null)
                        tra.Rollback();
                    Console.WriteLine(e.StackTrace);
                }
                finally
                {
                    session.Close();
                }
            }
        }
然后在cs文件里调用:
        //插入新的一行数据
        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).ReadOnly = true;
            //找到detailview中的行中控件的值
            string GroupName = Server.HtmlEncode(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);
            string ParentID = Server.HtmlEncode(((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text);
            string ModuleID = Server.HtmlEncode(((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text);
            string ModuleName = Server.HtmlEncode(((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text);
            string ModuleLocation = Server.HtmlEncode(((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text);            Group g = new Group();
            Module m = new Module();
            g.GroupName = GroupName;
            g.ParentID =int.Parse( ParentID);
            m.ModuleID = int.Parse(ModuleID);
            m.ModuleName = ModuleName;
            m.ModuleLocation = ModuleLocation;
            NHibernateDAO dao = new NHibernateDAO();
            dao.addGroupModule(g,m);
        }
它说未将对象设置到实例。我方法用的不对,还是怎么?

解决方案 »

  1.   

    HelpLink=null;
    InnerException=null;
    Declaring Type=null;
    Module.Assembly.EntryPoint=null;
    Reflected Type=null;
    Type Initializer=null;具体情况:
    “/”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 131:                {
    行 132:                    if (tra != null) tra.Rollback();
    行 133:                    throw ex;
    行 134:                }
    行 135:                finally
     源文件: E:\我的项目\权限管理\PowerManage\PowerWeb\GroupModule.aspx.cs    行: 133 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       PowerWeb.GroupModule.DetailsView1_ItemInserting(Object sender, DetailsViewInsertEventArgs e) in E:\我的项目\权限管理\PowerManage\PowerWeb\GroupModule.aspx.cs:133
       System.Web.UI.WebControls.DetailsView.OnItemInserting(DetailsViewInsertEventArgs e) +133
       System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +195
       System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +676
       System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +113
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
      

  2.   

    不好意思,这个问题已经解决了。是因为没有初始化。
    直接g.modules=new HashSet<Module>();
    m.groups=new HashSet<Group>();就行了。
    但是,我想问一个新问题:
    session.save(g);session.save(m);是否需要去掉前者?(Group是主控方)
    但是,不管去不去掉都会无法插入!怎么回事儿呢?是不是还需要配置什么?
      

  3.   

    在多对多关系上应该配置cascade="all"
      

  4.   

    这个我定义了的。
    具体情况是:
    could not retrieve snapshot: [PowerLib.Entities.Group#0][SQL: SELECT group_.GroupID, group_.GroupName as GroupName5_, group_.ParentID as ParentID5_ FROM Group group_ WHERE group_.GroupID=?]
      

  5.   

    哎,光是这样说,可能看不出来。我把配置文件发上来:
    Module.hbm.xml:
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="PowerLib" namespace="PowerLib.Entities">
      <class name="Module">
        <id name="ModuleID" column="ModuleID">
          <generator class="assigned" />
        </id>
        <property name="ModuleName"/>
        <property name="ModuleLocation"/>
        <set name="powers" table="modulepower">
            <key column="ModuleID"></key>
              <many-to-many column="PowerID" class="PowerLib.Entities.Power,PowerLib"></many-to-many>
        </set>
        <set name="groups" table="groupmodule">
          <key column="ModuleID"></key>
          <many-to-many column="GroupID" class="PowerLib.Entities.Group,PowerLib"></many-to-many>
        </set>
       </class>
    </hibernate-mapping>Group.hbm.xml:
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="PowerLib" namespace="PowerLib.Entities">
      <class name="Group">
        <id name="GroupID" column="GroupID" >
          <generator class="assigned" />
        </id>
        <property name="GroupName"/>
        <property name="ParentID"/>
        <set name="modules" table="groupmodule" inverse="true" cascade="all">
          <key column="GroupID"></key>
          <many-to-many column="ModuleID" class="PowerLib.Entities.Module,PowerLib">
          </many-to-many>
        </set>
      </class>
    </hibernate-mapping>
      

  6.   

    我现在就只执行这个操作了:
    //插入新的一行数据
            protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
            {
                ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).ReadOnly = true;
                //找到detailview中的行中控件的值
                string GroupName = Server.HtmlEncode(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);
                string ParentID = Server.HtmlEncode(((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text);
                string ModuleID = Server.HtmlEncode(((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text);
                string ModuleName = Server.HtmlEncode(((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text);
                string ModuleLocation = Server.HtmlEncode(((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text);
                ISession session=NHibernateHelper.GetSession();
                using (ITransaction tra=session.BeginTransaction())
                {
                    try
                    {
                        Group g = new Group();
                        g.modules = new HashedSet<Module>();
                        Module m = new Module();
                        m.groups = new HashedSet<Group>();
                        g.GroupName =GroupName;
                        g.ParentID = int.Parse(ParentID);
                        m.ModuleID = int.Parse(ModuleID);
                        m.ModuleName = ModuleName;
                        m.ModuleLocation = ModuleLocation;
                        m.groups.Add(g);
                        g.modules.Add(m);
                        //session.Save(g);
                        session.Save(m);
                        //session.SaveOrUpdate(g);
                        //session.SaveOrUpdate(m);
                        tra.Commit();
                    }
                    catch (Exception ex)
                    {
                        if (tra != null) tra.Rollback();
                        throw ex;
                    }
                    finally
                    {
                        session.Close();
                    }
                }
            }它要么说不能插入关系表groupmanage;要么就说不能插入group表。我要疯~