2009-07-18 4 views
0

J'utilise NHibernate 2.1CR1. J'ai modifié l'échantillon NHibernate d'ici http://weblogs.asp.net/pwilson/archive/2005/05/26/409042.aspx travailler avec la nouvelle versionLa propriété Formula (generated = always) génère un code SQL non valide pour la post-insertion. Sélectionnez

La classe

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; 
     } 
     } 
} 

est ici le fragment de cartographie correspondant

<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> 

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

Ici sont les traces de SQL Server: Quand j'appelle Session.get (1), le sql est correctement généré: ('' TestFormula '') comme 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 
[email protected]',N'@p0 int',@p0=1 

Mais quand je fais un insert, qui déclenche alors une autre sélection pour obtenir le valeurs générées Il génère un sql invalide pour la même propriété: contact_. comme formula0_

exec sp_executesql N'SELECT contact_. as formula0_ FROM Contacts 
contact_ WHERE [email protected]',N'@p0 int',@p0=14 

Qu'est-ce que je fais mal?

Répondre

0

Salut c'est un plan long, mais pourquoi ne pas définir généré = 'insérer' de cette façon l'id est actualisé sur le commit ... en espérant que je n'ai pas mal lu la fonctionnalité souhaitée

Questions connexes