Nhibernate Reference Documentation
Total Page:16
File Type:pdf, Size:1020Kb
NHibernate Reference Documentation Version: 5.3 Table of Contents Preface ............................................................................................................................................ix 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. Using a custom configuration provider .............................................................................. 15 3.6. Optional configuration properties ...................................................................................... 15 3.6.1. SQL Dialects ......................................................................................................... 23 3.6.2. Outer Join Fetching ............................................................................................... 25 3.6.3. Custom ICacheProvider ......................................................................................... 26 3.6.4. Query Language Substitution ................................................................................. 26 3.6.5. System level optional properties ............................................................................. 26 3.7. Logging ........................................................................................................................... 27 3.8. Implementing an INamingStrategy .................................................................................... 28 3.9. XML Configuration File ................................................................................................... 28 4. Persistent Classes ...................................................................................................................... 30 4.1. A simple POCO example .................................................................................................. 30 4.1.1. Declare properties for persistent fields .................................................................... 30 4.1.2. Implement a default constructor ............................................................................. 31 4.1.3. Provide an identifier property (optional) .................................................................. 31 4.1.4. Prefer non-sealed classes and virtual methods (optional) .......................................... 31 4.2. Implementing inheritance ................................................................................................. 31 4.3. Implementing Equals() and GetHashCode() ....................................................................... 31 4.4. Dynamic models .............................................................................................................. 32 4.5. Tuplizers ......................................................................................................................... 34 4.6. Lifecycle Callbacks .......................................................................................................... 35 4.7. IValidatable callback ........................................................................................................ 36 5. Basic O/R Mapping ................................................................................................................... 37 5.1. Mapping declaration ......................................................................................................... 37 5.1.1. XML Namespace .................................................................................................. 37 5.1.2. hibernate-mapping ................................................................................................. 38 5.1.3. class ..................................................................................................................... 38 5.1.4. subselect ............................................................................................................... 42 5.1.5. id .......................................................................................................................... 42 5.1.5.1. generator .................................................................................................... 43 5.1.5.2. Hi/Lo Algorithm ......................................................................................... 45 5.1.5.3. UUID Hex Algorithm ................................................................................. 45 NHibernate 5.3 ii NHibernate - Relational Persistence for Idiomatic .NET 5.1.5.4. UUID String Algorithm .............................................................................. 46 5.1.5.5. GUID Algorithms ....................................................................................... 46 5.1.5.6. Identity columns and Sequences .................................................................. 46 5.1.5.7. Assigned Identifiers .................................................................................... 46 5.1.5.8. Primary keys assigned by triggers ................................................................ 47 5.1.5.9. Enhanced identifier generators ..................................................................... 47 5.1.6. composite-id ......................................................................................................... 48 5.1.7. discriminator ......................................................................................................... 49 5.1.8. version (optional) .................................................................................................. 50 5.1.9. timestamp (optional) .............................................................................................. 51 5.1.10. property .............................................................................................................. 51 5.1.11. many-to-one ........................................................................................................ 55 5.1.12. one-to-one ........................................................................................................... 57 5.1.13. natural-id ............................................................................................................ 58 5.1.14. component, dynamic-component .......................................................................... 59 5.1.15. properties ............................................................................................................ 60 5.1.16. subclass .............................................................................................................. 60 5.1.17. joined-subclass .................................................................................................... 61 5.1.18. union-subclass ..................................................................................................... 62 5.1.19. join ..................................................................................................................... 63 5.1.20. key ..................................................................................................................... 64 5.1.21. column and formula elements ............................................................................... 64 5.1.22. map, set, list, bag ................................................................................................. 65 5.1.23. import ................................................................................................................. 65 5.1.24. any ..................................................................................................................... 65 5.2. NHibernate Types ...........................................................................................................