Nhibernate Mappings - nhibernate

Is there any way to get references to the mapping objects that NHibernate creates from the XML files? How about the ClassMap objects that FluentNhibernate creates? I wanted to create some query generation functions (for row counts, etc.) using this information. Since I went through the trouble of mapping it, I ought to have access to it in code, right?

Actually, Fluent ClassMaps are translated to XML.
The ISessionFactory exposes a GetAllClassMetadata method that is probably what you need.

Related

Nibernate / Fluent NHibernate : Using Filter with SubclassMap

Using Fluent NHibernate, I have succeeded in setting up a global Filter on my NHibernate session, and using it in ClassMap classes. The Filter WHERE clause is appended to queries using that map automagically as it should - as described in other posts on this forum.
Fluent NHibernate does not implement ApplyFilter<> of SubclassMap, it is only implemented for ClassMap. It was easy to do a test by adding a filter through the back door, by passing a MappingProviderStore to the SubclassMap Constructor, and adding the filter to that. Inspecting the resulting classes in the debugger shows that everything is populated identically to a ClassMap. However, not surprisingly, this didn't work.
Can someone tell me if Filters SHOULD work with SubclassMap in NHibernate itself?
Is this therefore something that might eventually be supported (e.g. by implementing SubclassMap.ApplyFilter<>) in Fluent NHibernate?
Using Fluent NHibernate 2.1, with NHibernate 3.1
I'm supposing that fluent call apply filter the :
as per this Jira Entry, at Oct 2012 the function is not yet availavle in NH.

How to create dynamic NHibernate mappings without generating HBM XML files?

I'm working on a dynamic application with NHibernate. My goal is to create dynamic entities (both class type and mapping xml file) based on some data. For example suppose I want to create a Person entity dynamically in runtime and on the fly.
I use Reflection.Emit to generate class type dynamically. For creating mapping dynamically I used Ayende's code.. But unfortunately this code does not work because mappings does not have Classes property. I tried to code as same as codes of Castle ActiveRecord and Fluent NHibernate but they generate HBM XML files. As I don't want to generate/create mapping files so I can not use these solutions.
Is there any way that like Ayende's solution not to be forced to generate HBM XML mapping files and just doing everything dynamically and in memory?
fluentnhibernate creates hbm in memory just to feed them to nhibernate. fluentnhibernate has the nice automapping feature with costumizable conventions, perfect for this situation. Also in FNH 2.0 they are working to skip hbm for better performance, but normally you'll never see the mappings outside memory.
Sample:
Assembly assembly = GetDynamicallyCreatedTypesAssembly();
ISessionFactory sf = Fluently.Configure()
.Database(...)
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assembly)))
.BuildSessionFactory();
NHibernate 3.2 has a mapping-by-code layer that does what you want.
I'm not sure if dynamic classes will work, but it doesn't hurt to try.

Can NHibernate use internal types?

I'm a little bit of an NHibernate noobie, and I was wondering if NHibernate can work with internal types. I have a project with a bunch of internal entities, and I would like to use NHibernate within the project to access my data store. If I put the mapping files in the same assembly (or is this even necessary?), will NHibernate work with my internal entities? Or do they need to be declare public?
It can. The mapping files location is not relevant.

Multiple ClassMaps classes in NHibernate

Is this possible in fluent nhibernate having multiple mappings for one table? Lets suppose i have a Users table.
Once i want it to be apped exactly like in file UserMap1.cs, and some times I would rather prefer mapping from UserMap2.cs.
I don't need to switch configurations while app is running. I just have to choose a proper one at the beginning.
Thanks in advance:-)
This might be a hack, but you could possibly put your two mappings into separate namespaces. Then you could add mappings from either one namespace or the other depending on your needs.
You're using Fluent NHibernate, so you're likely using the Mappings.FluentMappings object. I normally invoke AddAssemblyFrom (providing a type in the assembly containing mappings). You may need to invoke the Add or Add(Type type) method to add them invididually. My thought is to use MEF to attribute your ClassMap subclasses and add metadata in select cases to determine which ones to inject.

NHibernate: completely overriding base domain entity

I have a situation where I have a Common.Domain.Person and Specific.Domain.Person.
First one should be provided as a part of a common package.
Second one appears when common package has to be customized to fit the needs of specific project.
In the object model, it can be easily implemented with inheritance.
In the NH mapping, however, I have encountered a small problem.
I can create an NHibernate <subclass> mapping, but that would require me to use an discriminator. However, I know that if specific person class was inherited, then common class instances will never be used within this specific project.
What is the best way to implement this without adding discriminator column to the base class (since there are no different cases to discriminate)?
this is what i wanted and nhibernate supports it using xml entities. Unfortunately this feature has been borked since (at least) NH v2++.
see also Using Doctype in Nhibernate
A work-around could be to inject these properies programmaticaly when you create the SessionFactory (Dynamic Mapping)
see also http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx
Just map the Specific.Domain.Person and leave Common.Domain.Person unmapped.
If you are not saving instances of it, NHibernate does not need to know about it.