NHibernate - use WHERE clause on one-to-one mapping - nhibernate

Is it possible to somehow restrict a one-to-one mapping with a constraint other than property ref? Ideally, something along the lines of the class definition, for example, where you can add where="Condition=Value".
Thanks

You can use nhibernate filters for this: http://nhibernate.info/blogs/nhibernate/archive/2009/05/04/nhibernate-filters.aspx

Related

Fluent Nhibernate table-per-subclass with discriminator strategy

Is there anyway to use fluent in order to implement the table-per-subclass with discriminator strategy? From all of the documentation I've seen, the use of a discriminator actually tells fluent to use the table-per-class rather than table-per-subclass.
I am having a similar issue. So far the only option i have been able to think of is to put a property on the base class and have its set operation protected. Then any class that inherits the base, sets that property in its constructor. So far i have not found a better way.
in my case i have a class that references the base class and i want to query the type of subclass in that reference and filter on it in a query but found no way apart from what i just suggested.
If you have 1 table for each subclass then isn't the discriminator the name of the table... In other words you don't need to specify a discriminator if there is only 1 type per table.

Specifying the Table on a HasMany() relationship mapping in FluentNHibernate

I have a mapping in FluentNHibernate for a HasMany relationship and I'd like to specify a Table on it to override the default Table that nHibernate will look in to find those objects that I have many of. Does that make sense?
So lets say I have a table for Invoices and a table for InvoiceItems and lets say I have table called InvoiceItemsTwo.
I have a class for Invoice and a Class for InvoiceItems as well, and their mappings are pretty straight forward. I'd like to specify in my mapping for Invoice, that it should look for it's items in InvoiceItemsTwo instead of the default InvoiceItems.
So my mapping of that relationship looks like this
HasMany(c => c.InvoiceItems).Cascade.SaveUpdate().Table("InvoiceItemsTwo");
But this doesn't work. I keep getting an error from my website at runtime that says Invalid object name 'InvoiceItems'.
Why is it ignoring the fact that I am explicitly specifying the Table in my mapping on the relationship?
I tried dumping the mapping at run time and it's being setup something like this
<bag cascade="save-update" table="InvoiceItemsTwo">
Any ideas?
The table attribute applies only to many-to-many relationships, not one-to-many.
you can't specify a different table in your mapping class. Fluent NHibernate uses the class mapped on the property list (InvoiceItems).
If yoy want to use another class to map your details table you must create a InvoceItemsTwo class and map it in your master table class.
You could map the list as composite-element instead of a one-to-many relation and then map it to another table. But it is not a good idea. Consider that NH needs to know where to store an object which is in memory. So it may happen that the object is stored in the wrong table.
Either store all the InvoiceItems in separate tables using composite-element instead of one-to-many and components instead of many-to-one (however this is called in Fluent).
Or store all the InvoiceItems in the same table and use regular references.

Fluent Nibernate putting a where clause in the mapping

I've got two objects a parent and a child list. In my fluent nhibernate mapping for the parent I want to load the list of the children.
However I want this to be conditional, a column in the child table is called "IsDeleted" and I only want to return the children where "IsDeleted" is false.
Is it possible to set up a mapping to do this? If not is it possible to do it in just standard nhibernate?
Thanks
Yes, you can use a Where constraint in Fluent NHibernate to map this. Somehting like:
HasMany(x => x.Children).Where("IsDeleted = 0");
The Where constraint should use SQL syntax not HQL. For tables that allow soft deletes it's probably easier to map a view that filters the deleted records out.

Fluent nHibernate - unfriendly many-to-one reference name

I define my data model using Fluent nHibernate POCO classes + mappings. I'm also using nHiberate schema to create database schema.
All is working fine but there is one unpleasent fact.
When I use many-to-one reference referece is named by something similair to GUID instead of any descriptive name.
Here's a piece of SQL:
alter table [Odbiorca]
add constraint FK291D244B5D9E8115
foreign key (Adr_IdKraj)
references [Kraj]
I want nHiberate to generate something like Sql Studio does like [FK_Odbiorca_Kraj].
Is it doable by overridding mappings or by creating any convention?
I don't know Fluent, but with regular XML mapping you just can use the foreign-key attribute:
<many-to-one
name="Kraj"
class="Kraj"
column="Adr_IdKraj"
foreign-key="FK_Odbiorca_Kraj"/>

NHibernate mapping: one-to-many where

I need to restrict a one-to-many collection by a date column with a value specified on the parent element. The 'where' attribute on set or bag looks perfect for this.
NHibernate documentation describes the 'where' attribute as being for arbitrary SQL, so am I correct in assuming that I cannot use values from the parent class here as I would in HQL and must implement my own IUserCollection instead?
IMHO using a filter would be better because you could write the restriction in database agnostic HQL.