具有公式的属性(generated = always)为插入后选择生成无效的SQL

时间:2009-07-18 09:37:45

标签: nhibernate

我正在使用NHibernate 2.1CR1。我从这里修改了nhibernate样本 http://weblogs.asp.net/pwilson/archive/2005/05/26/409042.aspx工作 使用新版本

班级

namespace Wilson.NHibernate.Example
{
       public class Contact
       {
               private int id; // Database-Generated Key Field
               private string name;
               private Address address = new Address(); // Embedded Object Type
               private IList categories = new ArrayList(); // Many-To-Many
Relationship
               private IList details = new ArrayList(); // One-To-Many Relationship

       virtual public int Id
       {
                       get { return this.id; }
               }

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

       virtual public Address Address
       {
                       get { return this.address; }
               }

       virtual public IList Categories
       {
                       get { return this.categories; }
               }

       virtual public IList Details
       {
                       get { return this.details; }
               }

               public override string ToString() {
                       return this.name;
               }

       virtual public string PropertyWithFormula
       {
           get;
           set;
       }
       }
}

这是相关的映射片段

<class name="Wilson.NHibernate.Example.Contact,
WilsonNHibernateExample" table="Contacts" discriminator-value="?" >
               <id name="Id" column="ContactId" access="nosetter.camelcase" unsaved-
value="0">
                       <generator class="identity" />
               </id>
               <discriminator column="ContactType" />
               <property name="Name" column="ContactName" />
               <component name="Address" class="Wilson.NHibernate.Example.Address,
WilsonNHibernateExample" access="nosetter.camelcase">
                       <property name="Line" column="AddressLine" not-null="false" />
                       <property name="City" column="AddressCity" not-null="false" />
                       <property name="State" column="AddressState" not-null="false" />
                       <property name="Zip" column="AddressZip" not-null="false" />
               </component>

   <property name="PropertyWithFormula"  formula="('TestFormula')"
generated="always" />

               <bag name="Categories" table="CategoryContacts" cascade="none"
access="nosetter.camelcase" lazy="false" inverse="false">
                       <key column="ContactId" />
                       <many-to-many column="CategoryId"
class="Wilson.NHibernate.Example.Category, WilsonNHibernateExample" />
               </bag>
               <bag name="Details" cascade="all" access="nosetter.camelcase"
lazy="false" inverse="true">
                       <key column="ContactId" />
                       <one-to-many class="Wilson.NHibernate.Example.Detail,
WilsonNHibernateExample" />
               </bag>
               <subclass name="Wilson.NHibernate.Example.Person,
WilsonNHibernateExample" discriminator-value="P" />
               <subclass name="Wilson.NHibernate.Example.Business,
WilsonNHibernateExample" discriminator-value="B">
                       <property name="Company" column="CompanyName" />
               </subclass>
       </class>

=============================================== =======

以下是SQL Server的跟踪: 当我调用session.Get(1)时,正确生成了sql: (''TestFormula'')as formula0_0 _

exec sp_executesql N'SELECT contact0_.ContactId as ContactId2_0_,
contact0_.ContactName as ContactN3_2_0_, contact0_.AddressLine as
AddressL4_2_0_, contact0_.AddressCity as AddressC5_2_0_,
contact0_.AddressState as AddressS6_2_0_, contact0_.AddressZip as
AddressZip2_0_, contact0_.CompanyName as CompanyN8_2_0_,
(''TestFormula'') as formula0_0_, contact0_.ContactType as
ContactT2_2_0_ FROM Contacts contact0_ WHERE
contact0_.ContactId=@p0',N'@p0 int',@p0=1

但是当我做一个插入,然后激发另一个选择来获得 生成的值 它为同一属性生成无效的sql:contact_。如 formula0 _

exec sp_executesql N'SELECT contact_. as formula0_ FROM Contacts
contact_ WHERE contact_.ContactId=@p0',N'@p0 int',@p0=14

我做错了什么?

1 个答案:

答案 0 :(得分:0)

嗨,这是一个很长的镜头,但为什么不设置generated ='insert',这样在提交时刷新id ...希望我没有误读所需的功能