《NHibernate in Action》读书笔记【二】
2010-11-08 16:30
423 查看
Basic Configuration
1.Creating a SessionFactory :
It's event possible to embed XML mapping files inside .NET assemblies. You have to tell the compiler that each of these files is an embedded resource.
The AddClass() method assumes that the name of the mapping file ends with the .hbm.xml extension and is embedded in the same assembly as the mapped class file.
If you want to add all mapped classes
(with .NET attributes) in an assembly, you can use an overload of the method HbmSerializer.Serialize()
; or, if you want to add all mapping files
embedded in an assembly, you can use the method AddAssembly
():
ISessionFactory sessionFactory = new Configuration()
.Configure()
.AddInputStream( // .NET Attributes
HbmSerializer.Default.Serialize(typeof(Model.Item).Assembly) )
.AddAssembly( typeof(Model.Item).Assembly ) // XML
.BuildSessionFactory();
2.Configuring the ADO.NET database access
connection
.provider :specifies the name of the .NET class implementing the IConnectionProvider interface.
dialect
:specifies the name of the .NET class implementing the database Dialect .
connection.driver_class
: specifies the name of the .NET class implementing the ADO.NET Driver.
connection.connection_string
: specifies a standard ADO.NET connection string , used to create a database connection.
Setting the property show_sql
to the value true enables logging of all generated SQL to the console.
1.Creating a SessionFactory :
private static ISessionFactory sessionFactory = null; public static ISessionFactory MySessionFactory { get { if(sessionFactory==null) { Configuration cfg = new Configuration(); cfg.Configure(); cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(Employee))); // Or cfg.AddXmlFile("Employee.hbm.xml"); sessionFactory = cfg.BuildSessionFactory(); } } }
It's event possible to embed XML mapping files inside .NET assemblies. You have to tell the compiler that each of these files is an embedded resource.
ISessionFactory sessionFactory = new Configuration() .Configure() .AddClass( typeof(Model.Item) ) .AddClass( typeof(Model.User) ) .AddClass( typeof(Model.Bid) ) .BuildSessionFactory();
The AddClass() method assumes that the name of the mapping file ends with the .hbm.xml extension and is embedded in the same assembly as the mapped class file.
If you want to add all mapped classes
(with .NET attributes) in an assembly, you can use an overload of the method HbmSerializer.Serialize()
; or, if you want to add all mapping files
embedded in an assembly, you can use the method AddAssembly
():
ISessionFactory sessionFactory = new Configuration()
.Configure()
.AddInputStream( // .NET Attributes
HbmSerializer.Default.Serialize(typeof(Model.Item).Assembly) )
.AddAssembly( typeof(Model.Item).Assembly ) // XML
.BuildSessionFactory();
2.Configuring the ADO.NET database access
<?xml version="1.0" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property> <property name="dialect"> NHibernate.Dialect.MsSql2000Dialect </property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver </property> <property name="connection.connection_string"> Data Source=(local); Initial Catalog=nhibernate; Integrated Security=SSPI </property> </session-factory> </hibernate-configuration>
connection
.provider :specifies the name of the .NET class implementing the IConnectionProvider interface.
dialect
:specifies the name of the .NET class implementing the database Dialect .
connection.driver_class
: specifies the name of the .NET class implementing the ADO.NET Driver.
connection.connection_string
: specifies a standard ADO.NET connection string , used to create a database connection.
Setting the property show_sql
to the value true enables logging of all generated SQL to the console.
相关文章推荐
- 《NHibernate in Action》读书笔记【一】
- NHibernate in Action: write POCOs【Association】【2】
- Cpp Concurrency In Action 读书笔记 第一章
- 《Lucene In Action》读书笔记
- 《Spring in Action》第四版第一章《将Spring付诸实践》读书笔记(二)
- Sping in Action读书笔记之一----------Bean的各种注入操作
- Go in Action - Manning 2016(读书笔记)
- Spring In Action(第三版)读书笔记
- NHibernate in action翻译——第二章 2.1节 Nhibernate的“Hello World”
- NHibernate in action翻译——第二章 2.2节 理解NHibernate的结构
- 《R in Action》读书笔记(3) 数据变换
- 《Spring In Action》 读书笔记(5) -- @autowire,@Qualifier减少xml配置
- 读书笔记——《C++ Concurrency IN ACTION》
- 《R in Action》读书笔记(2)
- Mahout in Action 读书笔记chapter3 推荐数据的表示
- Junit in Action 读书笔记序
- 《Machine Learning in Action》 读书笔记之一:K近邻分类器算法
- NHibernate in action翻译——第二章 2.3(基本配置)
- 《C++ Concurrency in Action》读书笔记一 多线程与线程管理