Surance Center

Nhibernate 1.2.0.4GA 在VS2005 + sqlServer2005下测试通过的代码

Download:
/Files/xxpyeippx/Solution1.rar


POCO

namespace Cat
{
    
public class Cat
    
{
        
private string id;
        
private string name;
        
private char sex;
        
private float weight;

        
public Cat()
        
{
        }


        
public virtual string Id
        
{
            
get return id; }
            
set { id = value; }
        }


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


        
public virtual char Sex
        
{
            
get return sex; }
            
set { sex = value; }
        }


        
public virtual float Weight
        
{
            
get return weight; }
            
set { weight = value; }
        }

    }

}



Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"
    namespace
="Cat" assembly="Cat">

  
<class name="Cat" table="Cat">

    
<!-- A 32 hex character is our surrogate key. It's automatically
            generated by NHibernate with the UUID pattern. 
-->
    
<id name="Id">
      
<column name="CatId" sql-type="char(32)" not-null="true"/>
      
<generator class="uuid.hex" />
    
</id>

    
<!-- A cat has to have a name, but it shouldn' be too long. -->
    
<property name="Name">
      
<column name="Name" length="16" not-null="true" />
    
</property>
    
<property name="Sex" />
    
<property name="Weight" />
  
</class>

</hibernate-mapping>



Config:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 


  
<configSections>
    
<section
        
name="hibernate-configuration"
        type
="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
        
/>
  
</configSections>

  
<!-- Add this element -->
  
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    
<session-factory>
     
      
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      
<property name="connection.connection_string">Data Source=(local);Initial Catalog=Cat;User ID=sa;Password=xxpyeippx@126.com</property>

      
<mapping assembly="Cat" />
    
</session-factory>
  
</hibernate-configuration>







</configuration>



Test:
using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using Cat;


namespace ConsoleTest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{

            Configuration cfg 
= new Configuration();
            cfg.Configure();
           
// cfg.AddFile(@"E:\Nhibernate\NhibernateTry\Solution1\ConsoleTest\Cat.hbm.xml");
            ISessionFactory f = cfg.BuildSessionFactory();
            ISession s 
= f.OpenSession();
           ITransaction t 
= s.BeginTransaction();

            Cat.Cat c 
= new Cat.Cat();
            c.Name 
= "surance";
            c.Sex 
= 'F';
            c.Weight 
= 7.8F;
            

            s.Save(c);






            IQuery query 
= s.CreateQuery("select c from Cat as c where c.Sex = :sex");
            query.SetCharacter(
"sex"'F');
            
foreach (Cat.Cat cat in query.Enumerable())
            
{
                Console.Out.WriteLine(
"Female Cat: " + cat.Name);
            }





            t.Commit();
            s.Close();





           
        }

    }

}

posted @ 2007-08-21 16:05  xxp  阅读(498)  评论(0编辑  收藏  举报
Surance Center