Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
NHibernate(one-to-many)的问题

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
 <class name="RG.SuperStarOA.Customers.Data.Customers,WebApplication1" table="Customers">
  <id name="CustomerID" column="CustomerID" type="String" length="5">
   <generator class="assigned" />
  </id>
  <set name="Orders" >
   <key column="CustomerID" />
   <one-to-many class="RG.SuperStarOA.Orders.Data.Orders,WebApplication1" />
  </set>
  <property name="CompanyName" column="CompanyName" type="String" length="40"></property>
  <property name="ContactName" column="ContactName" type="String" length="30"></property>
  <property name="ContactTitle" column="ContactTitle" type="String" length="30"></property>
  <property name="Address" column="Address" type="String" length="60"></property>
  <property name="City" column="City" type="String" length="15"></property>
  <property name="Region" column="Region" type="String" length="15"></property>
  <property name="PostalCode" column="PostalCode" type="String" length="10"></property>
  <property name="Country" column="Country" type="String" length="15"></property>
  <property name="Phone" column="Phone" type="String" length="24"></property>
  <property name="Fax" column="Fax" type="String" length="24"></property>
 </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
 <class name="RG.SuperStarOA.Orders.Data.Orders,WebApplication1" table="Orders">
  <id name="OrderID" column="OrderID" type="Int32" length="4">
   <generator class="identity" />
  </id>
  <many-to-one name="Customers" column="CustomerID" class="RG.SuperStarOA.Customers.Data.Customers,WebApplication1" />
  <property name="CustomerID" column="CustomerID" type="String" length="5"></property>
  <property name="EmployeeID" column="EmployeeID" type="Int32" length="2"></property>
  <property name="OrderDate" column="OrderDate" type="DateTime" length="4"></property>
  <property name="RequiredDate" column="RequiredDate" type="DateTime" length="4"></property>
  <property name="ShippedDate" column="ShippedDate" type="DateTime" length="4"></property>
  <property name="ShipVia" column="ShipVia" type="Int32" length="4"></property>
  <property name="Freight" column="Freight" type="Decimal" length="4"></property>
  <property name="ShipName" column="ShipName" type="String" length="40"></property>
  <property name="ShipAddress" column="ShipAddress" type="String" length="60"></property>
  <property name="ShipCity" column="ShipCity" type="String" length="15"></property>
  <property name="ShipRegion" column="ShipRegion" type="String" length="15"></property>
  <property name="ShipPostalCode" column="ShipPostalCode" type="String" length="10"></property>
  <property name="ShipCountry" column="ShipCountry" type="String" length="15"></property>
 </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
        <class name="RG.SuperStarSystem.Shippers.Data.Shippers,WebApplication1" table="Shippers">
            <id name="ShipperID" column="ShipperID" type="Int32" length="4">
                <generator class="identity"/>
            </id>
            <property name="CompanyName" column="CompanyName" type="String" length="40"></property>
            <property name="Phone" column="Phone" type="String" length="24"></property>
        </class>
    </hibernate-mapping>

//下面这段代码执行就完全没有问题
private void Button3_Click(object sender, System.EventArgs e)
  {
   NHibernate.Cfg.Configuration c = new NHibernate.Cfg.Configuration();
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Customers.xml"));
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Orders.xml"));
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Shippers.xml"));
   
   ISession vSession= c.BuildSessionFactory().OpenSession();
   ITransaction vTransaction = vSession.BeginTransaction();

   try 
   { 
    IList data = vSession.Find("from Shippers");

    DataGrid1.DataSource = data;
    DataGrid1.DataBind();
    
    vTransaction.Commit(); 
   }
   catch(Exception ex) 
   { 
    vTransaction.Rollback(); 
    Response.Write(ex.Message);
   } 
   finally 
   { 
    vSession.Close(); 
   }
  }


//这段代码去老是提示:Could not execute query 
private void Button2_Click(object sender, System.EventArgs e)
  {
   NHibernate.Cfg.Configuration c = new NHibernate.Cfg.Configuration();
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Customers.xml"));
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Orders.xml"));
   c.AddXmlFile(System.Web.HttpContext.Current.Server.MapPath("Shippers.xml"));
   
   ISession vSession= c.BuildSessionFactory().OpenSession();
   ITransaction vTransaction = vSession.BeginTransaction();

   try 
   { 
    IList data = vSession.Find("from Customers");

    DataGrid1.DataSource = data;
    DataGrid1.DataBind();
    
    vTransaction.Commit(); 
   }
   catch(Exception ex) 
   { 
    vTransaction.Rollback(); 
    Response.Write(ex.Message);
   } 
   finally 
   { 
    vSession.Close(); 
   }
  }

我测试来测试去,都是觉得是
 <set name="Orders" >
   <key column="CustomerID" />
   <one-to-many class="RG.SuperStarOA.Orders.Data.Orders,WebApplication1" />
  </set>
或者,写查询语句有问题?
谁能解决,拜托~`



相关文章

相关软件