What is NHibernate?
NHibernate is an ORM (Object Relational Mapping) system. Providing an xml file which maps table schema to the class members, NHibernate is capable of managing a persistent data objects for respective mapped classes. As we already realised there is a gap between object oriented entity modal and relational data modal. NHibernate fills this gap providing HQL (Hibernate Query Language) to query objects rather than relational queries.
How it works?
Key object is ISession. ISession holds a persistent pool of data objects. ISessionFactory is the origin of ISession. ISessionFactory uses XML mappings (as a first level cache) and connection provider to create ISession. Connection provider uses IDriver to create connections. IDriver uses NHibernate configuration (XML file containing database query string and NHibernate settings) to create client specific connection string and manage optimistic behaviour based on the client.
- Factory for Session
- Client of IConnection Provider
- Has immutable cache compiled mappings (level1) derived from XML mappings
- Optional second level cache of data which might be useful between transactions.
- Short lived objects between application and data store.
- Factory for transaction.
- First level cache of persistent objects
- Used in object graphs and navigating
- Can expand across multiple transactions
- In the session all the persistent objects get associated with a CLR identity in the object map which is equivalent to the persistent identity.
- A short lived object that perform unit of work
- In meaning of it terms data update/Inset or delete
- While transaction is on progress all the underline relations are read-only
- Get generated from ISession
- ISession might span several ITransactions
- Not exposed to the application but can be extended by the developer
- Factory for ADO.NET connections/commands
- Abstracts application from vender specific implementation of IDbConnection/IDbCommand.
Can be extended by developer
- Abstracts capabilities of different Ado.NET providers
- Handles parameter naming / type conversions and other supported Ado.NET features.
NHibernate settings and appropriate connection string to the data store. Includes driver classes, dialect
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string_name">SqlServer</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
In a formal notation, mappings are the relationship between relation structure and class structure. In simple terms mappings are the relationship between class name and table name plus table columns and class properties.
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ActiveTest" namespace="ActiveTest"> <class name="Product"> <id name="Id" /> <property name="Name" /> <property name="Description" /> </class> </hibernate-mapping>
Objects and collection states:
- Transient: - The transient has not and never been associated with a NHibernate session (persistent context). It has no identity (primary key value)
- Persistent: - Data objects that are currently associated with NHibernate session (persistent context) called as persistent objects. They are associated with primary key value. Object may represent a row of a relation. Single primary key value can represent one and only one object in the persistent context. For a persistent object NHibernate guarantees that the persistent identity is equivalent to the CLR identity.
- Detached: - Objects been associated with persistent context but now the context is closed. For detached objects NHibernate makes no guarantees about the association between persistent context identity and the CLR identity.