Nhibernate Reference Documentation
Total Page:16
File Type:pdf, Size:1020Kb
NHibernate Reference Documentation Version: 5.0 Table of Contents Preface ..........................................................................................................................................viii 1. Quick-start with IIS and Microsoft SQL Server ..........................................................................1 1.1. Getting started with NHibernate .......................................................................................... 1 1.2. First persistent class ........................................................................................................... 2 1.3. Mapping the cat ................................................................................................................. 2 1.4. Playing with cats ................................................................................................................ 3 1.5. Finally ...............................................................................................................................6 2. Architecture ................................................................................................................................7 2.1. Overview ...........................................................................................................................7 2.2. Instance states .................................................................................................................... 9 2.3. Contextual Sessions .......................................................................................................... 10 3. ISessionFactory Configuration .................................................................................................. 11 3.1. Programmatic Configuration ............................................................................................. 11 3.2. Obtaining an ISessionFactory ............................................................................................ 12 3.3. User provided ADO.NET connection ................................................................................ 12 3.4. NHibernate provided ADO.NET connection ...................................................................... 12 3.5. Optional configuration properties ...................................................................................... 15 3.5.1. SQL Dialects ......................................................................................................... 21 3.5.2. Outer Join Fetching ............................................................................................... 24 3.5.3. Custom ICacheProvider ......................................................................................... 24 3.5.4. Query Language Substitution ................................................................................. 24 3.6. Logging ........................................................................................................................... 24 3.7. Implementing an INamingStrategy .................................................................................... 25 3.8. XML Configuration File ................................................................................................... 25 4. Persistent Classes ...................................................................................................................... 27 4.1. A simple POCO example .................................................................................................. 27 4.1.1. Declare properties for persistent fields .................................................................... 27 4.1.2. Implement a default constructor ............................................................................. 28 4.1.3. Provide an identifier property (optional) .................................................................. 28 4.1.4. Prefer non-sealed classes and virtual methods (optional) .......................................... 28 4.2. Implementing inheritance ................................................................................................. 28 4.3. Implementing Equals() and GetHashCode() ....................................................................... 28 4.4. Dynamic models .............................................................................................................. 29 4.5. Tuplizers ......................................................................................................................... 30 4.6. Lifecycle Callbacks .......................................................................................................... 31 4.7. IValidatable callback ........................................................................................................ 32 5. Basic O/R Mapping ................................................................................................................... 33 5.1. Mapping declaration ......................................................................................................... 33 5.1.1. XML Namespace .................................................................................................. 33 5.1.2. hibernate-mapping ................................................................................................. 34 5.1.3. class ..................................................................................................................... 34 5.1.4. subselect ............................................................................................................... 36 5.1.5. id .......................................................................................................................... 37 5.1.5.1. generator .................................................................................................... 37 5.1.5.2. Hi/Lo Algorithm ......................................................................................... 38 5.1.5.3. UUID Hex Algorithm ................................................................................. 39 5.1.5.4. UUID String Algorithm .............................................................................. 39 5.1.5.5. GUID Algorithms ....................................................................................... 39 NHibernate 5.0 ii NHibernate - Relational Persistence for Idiomatic .NET 5.1.5.6. Identity columns and Sequences .................................................................. 39 5.1.5.7. Assigned Identifiers .................................................................................... 40 5.1.5.8. Enhanced identifier generators ..................................................................... 40 5.1.6. composite-id ......................................................................................................... 41 5.1.7. discriminator ......................................................................................................... 42 5.1.8. version (optional) .................................................................................................. 43 5.1.9. timestamp (optional) .............................................................................................. 43 5.1.10. property .............................................................................................................. 44 5.1.11. many-to-one ........................................................................................................ 46 5.1.12. one-to-one ........................................................................................................... 47 5.1.13. natural-id ............................................................................................................ 49 5.1.14. component, dynamic-component .......................................................................... 49 5.1.15. properties ............................................................................................................ 50 5.1.16. subclass .............................................................................................................. 51 5.1.17. joined-subclass .................................................................................................... 51 5.1.18. union-subclass ..................................................................................................... 52 5.1.19. join ..................................................................................................................... 53 5.1.20. map, set, list, bag ................................................................................................. 54 5.1.21. import ................................................................................................................. 54 5.2. NHibernate Types ............................................................................................................ 54 5.2.1. Entities and values ................................................................................................. 54 5.2.2. Basic value types ................................................................................................... 54 5.2.3. Custom value types ............................................................................................... 58 5.2.4. Any type mappings ................................................................................................ 59 5.3. SQL quoted identifiers ...................................................................................................... 60 5.4. Modular mapping files .....................................................................................................