FluentNHibernate joined-subclass - nhibernate

What is the current method of implementing the joined-subclass structure with FluentNHibernate? By "current", I mean by not using deprecated methods such as JoinedSubClass or AddPart.
Thanks!

The recommended way to map a joined-subclass is to inherit from SubclassMap much in the same way you inherit from ClassMap for your non-subclass entities.
It's described in the fluent mapping subclasses section on the wiki. For automapping please refer to Automapping Inheritance.

Related

Fluent NHibernate mapping of a bi-directional, many-to-many association using an association class

I have a bi-directional, many-to-many association between EntityA and EntityB and I’m using an association class, EntityABLink, to model this because there are other attributes about the relationship that I need to track. In addition, I also have another class that holds a reference to a specific relationship between EntityA and EntityB so I treat the association class as a full-fledged entity.
In EntityA I have a read-only property that returns a list of associated EntityB objects and, likewise, in EntityB I have a read-only property that returns a list of associated EntityA objects. Note that, for these properties, I’m hiding the fact that the association is implemented via an association class. (I also have dedicated methods for updating the relationships that hide the implementation.) Behind the scenes in both EntityA and EntityB, I have private collections of type EntityABLink.
Since a picture is worth a thousand words, here is what I have described so far:
(Note again that the public, read-only properties on EntityA and EntityB are not of the same type as the private members that back them up.)
So far, so good. Now I want to persist these objects to a database using Fluent NHibernate automapping overrides. When it comes to mapping, I like to think of the above using this functionally equivalent representation:
From this diagram, it’s clear that what I really need is two bi-directional one-to-many relationships.
In mapping the above, I figure that I need something like this:
In the EntityA automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityA>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate();
mapping.IgnoreProperty(x => x.EntityBList);
In the EntityB automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityB>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate();
mapping.IgnoreProperty(x => x.EntityAList);
In the EntityABLink automapping override:
mapping.References<EntityA>(x => x.EntityA).Not.Nullable();
mapping.References<EntityB>(x => x.EntityB).Not.Nullable();
When I try this, however, I get the following error:
"Could not find a getter for property '_ AssociationList’ in class 'EntityB'."
I must have something wrong with my mappings, but I’m not sure what. Any ideas?
I got it working now. So here's the trick... I reverted back to Fluent NHibernate version 1.1 (specifically 1.1.0.685). Then, although the mapping examples that use "Reveal.Member" don't show it as being necessary, I added "Access.Field()" to the mapping for both EntityA._AssociationList and EntityB._AssociationList. Here are the working mappings.
In the EntityA automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityA>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate().Access.Field();
mapping.IgnoreProperty(x => x.EntityBList);
In the EntityB automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityB>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate().Access.Field();
mapping.IgnoreProperty(x => x.EntityAList);
In the EntityABLink automapping override:
mapping.References<EntityA>(x => x.EntityA).Not.Nullable();
mapping.References<EntityB>(x => x.EntityB).Not.Nullable();
Once it was working in FNH 1.1, I tried upgrading to FNH 1.2. No good. I tried 1.2.0.694 as well as 1.2.0.712 and both of these still give the incorrect error message that a different "entity" (which is actually an enum!) doesn't have an Id mapped.
Fluent NHibernate is a wonderful tool so I hope that the bug in the latest version gets fixed. :-)

NHIbernate 3 can't automap XmlDocument property

I'm trying to map a type where one of the properties is an XmlDocument but I get this error:
NHibernate.MappingException : An association from the table ChangeLog_TestAuditHistory refers to an unmapped class: System.Xml.XmlDocument
I am using Fluent NHibernate automappings. NHibernate version 3.0.0.4000 and Fluent NHibernate version 1.2.0.694. I know NHibernate is supposed to support xml columns, but I've never seen any examples using auto mappings.
That's probably a bug in Fluent, or a wrong convention... you probably need to tell it that XmlDocument is not an entity.
If you map it as a regular property, NH will use the correct mapping by default.

nhibernate automapping with union-subclass option

I want to (auto)map a base class and its derived class to two different tables, as described here by ayende (unioned subclasses).
however, according to fluent nHibernate's documentation, I don't see a way do do that.
the property in IAutoMappingOverride they refer to is "IsDiscriminated", but that only difrrentiates between table-per-heirarchy and table-per-subclass.
Is it possible that automapping doesn't support unioned subclasses? and if so- can anyone suggest a workaround?
thanks,
Jhonny

How to work with IUserType in Automapping Fluent NHibernate?

i have a custom type that implemented IUserType.
one of my domain classes have a property of this custom type.
now i want to make Automapping work with this domain, it says that "
An association from the table xyz refers to an unmapped class: PersianDate"
PersianDate is my custom type .
how should i tell to automapping that this is not a reference, it is a custom user type that is fitted into a string column !!!
i searched everything that was in the internet , i think there is something wrong here
would you please help me to fix it
i found the answer Here, i should use a UserTypeConvention in Automapping conventions

Is it possible to set a default value on a property with NHibernate

I'm using Fluent NHibernate for my mappings and the SchemaExport class to the database schema.
Is it possible with NHibernate to set a default value for a property/column in the generated database schema?
Not to my knowledge, no - not in the generated schema.
You can just set the property in your constructor though.
It is definitely supported, both in XML mapping and in Fluent NHibernate.
For XML mapping, use the <column> child element of <property>, documented here.
For Fluent NHibernate, there's a method for fluent mapping, as described in this SO answer.