NHibernate Mapping Samples – 50 Samples of NHibernate Mapping

NHibernate Mapping Samples application from Devart demonstrates 50 different mapping cases and how they are mapped using both fluent and XML mapping.

NHibernate is a widely used open-source ORM solution for .NET Framework that is well-known for its great flexibility in mapping .NET classes to database tables, because it supports almost all kinds of such mapping ever needed by developers. However, its is also well-known for its complex mapping both fluent and XML. If you are a beginner NHibernate developer, download our NHibernate Mapping Samples application to get acquainted with NHibernate mapping.

For example, the Simple One-To-Many sample demonstrates mapping of a on-to-many association between two entities of a model. In the database, such an association is arranged as a master table with a primary key and a detail table with a foreign key, referencing the primary key of the master table. You can view generated entities, XML and fluent mapping, as well as the database script for this and many other samples in our application.

How to Use

NHibernate Mapping Samples application does not require any third-party software except for the .NET Framework. Simply launch the application and explore our NHibernate mapping samples. You can study the classes and mapping in the application window or open a Visual Studio project for each sample to check how it works in a real application. Database creation script is available for each sample. You can also easily access NHibernate documentation page relevant to the opened sample. If you have Entity Developer for NHibernate, you may also open Entity Developer models that were used for generating mapping and classes for samples.

The window of the NHibernate Mapping Samples application consists of four parts.

NHibernate Mapping Samples screenshot
  1. The Samples tree lists all the available mapping cases.
  2. The Mapping box contains the sample of mapping for the selected mapping case. It has two tabs - XML and Fluent, containing the corresponding mapping code.
  3. The Code box contains the code of the classes, that are mapped.
  4. The Description box contains the short description of the selected mapping case and links that allow you to open the sample in Visual Studio, open model in Entity Developer, view the database script for this sample, or open the corresponding page in NHibernate documentation.

Here are some tips on how to work with our NHibernate Mapping Samples application:

  • To open a sample, click the corresponding node in the Samples tree (1).
  • To open the sample project in Visual Studio, click the "Open the sample in Visual Studio" link in the Description box (4).
  • To open the sample model in Entity Developer, click the "Open Entity Developer model" link in the Description box (4). You will be proposed to download the Trial Edition of Entity Developer if you do not have Entity Developer installed.
  • To view database objects creation script, click the "View database objects creation script" link in the Description box (4).
  • To open the corresponding page in NHibernate documentation, click the "view relevant NHibernate help topic" link in the Description box (4).
  • Mapping is displayed in the Mapping box (2). Note that you can switch between the fluent and XML mapping by selecting the corresponding tabs.

List of the Samples

Class Mapping

  • Simple Class (tags used: <class>, <id>, <property>)
  • Computed Property (formula attribute of the <property> tag)
  • Version Property(tags used: <version>)
  • Enum Type Property (type attribute of the <property> tag)
  • Join Tables (tags used: <join>)
  • where (where attribute of the <class> tag)
  • filter (tags used: <filter>, <filter-def>, <filter-param>)

Class Identifier

  • Simple Identifier (tags used: <id>)
  • Composite Identifier (tags used: <composite-id>)

Associations

  • One-to-Many(Collection mappings)
    • Simple One-to-Many (tags used: <set>, <key>, <one-to-many>, <many-to-one>)
    • Simple ZeroOrOne-to-Many (tags used: <set>, <key>, <one-to-many>)
    • Unique Key (tags used: <set>, <key>, <one-to-many>; property-ref attribute of the <key> tag)
    • Composite Identifier (tags used: <set>, <key>, <composite-id>)
    • Component Identifier (tags used: <set>, <key>, class attribute of the <composite-id> tag)
    • Component Unique Key (tags used: <component>, <set>, <key>, property-ref attribute of the <key> tag)
    • Join Tables (tags used: <join>)
    • Order By (order-by attribute of the <set> tag, <bag>, <map>, <list>, <array>)
    • Where (where attribute of the <set> tag, <bag>, <map>, <list>, <array>)
    • Filter (tags used: <filter>, <filter-def>, <filter-param>)
    • Collection Of Components (tags used: <composite-element>)
    • Collection Types
      • Set (tags used: <set>)
      • Bag (tags used: <bag>)
      • Map (tags used: <map>, <index>)
      • Map With Complex Index (tags used <map>, <composite-index>)
      • List (tags used: <list>, <index>)
      • Array (tags used <array>, <index>)
  • One-To-One
    • Simple One-to-One (tags used: <one-to-one>)
    • Component Identifier (tags used: <one-to-one>, <composite-id>)
    • Unique Foreign Key (property-ref attribute of the <one-to-one> tag и tags used: <many-to-one>)
    • Composite Unique Foreign Key (tags used: <composite-id>; property-ref attribute of the <one-to-one>; <many-to-one>)
  • Many-to-Many
    • Simple Many-to-Many (tags used: <many-to-many>)
    • Composite Identifier (tags used: <many-to-many>, <composite-id>)
    • Unique Keys (property-ref attribute of the <many-to-many> tag; property-ref attribute of the <key> tag)
    • Component Unique Keys (tags used: <component>; property-ref attribute of the <many-to-many> tag; property-ref attribute of the <key> tag)
    • Extra Columns Component (tags used: <composite-element>, <many-to-one>)
    • Entity Index Map (tags used: <map>, <many-to-many>, <index-many-to-many>)
    • IdBag Collection (tags used: <idbag>, <many-to-many>, <collection-id>)

Components

  • Component Properties (tags used: <component>, <property>)
  • Embeded Components (tags used <component>, <property>)
  • Composite Identifier (tags used: <composite-id>)
  • Navigation properties
    • Many-to-one (tags used: <component>, <many-to-one>)
    • Many-to-many (tags used: <component>, <many-to-many>)

Inheritances

  • Table Per Hierarchy (tags used: <discriminator>, <subclass>)
  • Table Per Type (tags used: <joined-subclass>)
  • Table Per Concrete Class (tags used: <union-subclass>)

Named SQL Query

  • Without Result (tags used: <sql-query>, <query-param>)
  • Scalar Result (tags used: <sql-query>, <return-scalar>)
  • Entity Result (tags used: <sql-query>, <return>, <return-property>, <query-param>)
  • Complex Type Result (tags used: <sql-query>, <return>, <return-property>, <query-param>)
  • Execute Stored Function (tags used: <sql-query> and its attribute callable; <query-param>)

The code and mapping of these samples was generated by Entity Developer for NHibernate. Entity Developer for NHibernate, being the best NHibernate designer to-date, allows you to create NHibernate models fastly in a convenient GUI environment and generate code and mapping for them.

Each of the samples is a complete separate Visual Studio project that demonstrates the implementation and usage of the mapping. You can open them in Visual Studio and study how they work. You can build and debug them to study how they work on the run, view results they return, or even modify them to study how to apply such mapping case in your projects. SQLite is used as a database for the samples, and dotConnect for SQLite is used to access the database. You don't need to install any additional software, the samples contains all the necessary files to be compiled and run. However you may download the trial edition of Entity Developer to view or edit mapping visually.