Fluent Nhibernate table-per-subclass with discriminator strategy - nhibernate

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.

Related

Select generic ndb Model by id

The model ids are unique in all clases.
I want to know how to use "get_by_id()" without know the model class name.
For example, in
MyModel.get_by_id(42)
need to use "MyModel" class
and in this other way
ndb.Key(MyModel, id).get()
need the class name too.
I think that could be posible something without the class name. I am rigth?
Thanks for your answers.
No, you can't. First, the auto-assigned IDs are not unique across models. Second, entities have a "key" that is composed of one or more (model, ID) pairs -- the model (== class name) is an essential part of the key.
You may be confused by queries. It is possible (though not recommended) to have a query for "all entities", i.e. without specifying the model. But this will obviously not scale.

Best way to map a hidden property in NHibernate (fluent)

I know this question could lead to a subjective answer, but I'd like to get an opinion from someone else.
Some background
Currently I have a class that maps a private string property to a geometry column in a PostgreSQL (PostGIS) database table along with a public string for WKT. The WKT is used by PostGIS to automatically update the geometry column, using a trigger. As I don't want to include any spatial references in my domain model, all querying is done using WKT strings and a custom spatial criterion, which wraps the WKT in a spatial PostGIS function and queries the private geometry property column reference. All of this works as expected.
The question
Since I need the column reference, I also need the property in my domainmodel, for NHibernate to map to, so I was wondering, what the best solution would be, for NHibernate to never select this property.
My current solution looks as follows with Fluent NHibernate:
Map(Reveal.Member<LocationReference>("Geometry"), "geometry")
.Generated.Always()
.ReadOnly()
.LazyLoad();
This does the trick, and when I select the entity, I won't get the property, unless I manually load it (which isn't possible through the lambda extensions). Unfortunately I would still be able to do a Criteria or HQL query for the property.
So are there any ways for me to do anything that prevents NHibernate from being able to do ever include the column in a select? Or is the above solution the only way to at least ignore the column when selecting with Query<> or QueryOver<>?
Well, I ended up removing the mapped property, because, even though it was lazy, NHibernate would sometimes load it anyway. What I did was actually a bit of a hack. I needed the alias for a property from the same class, so I used the mapping from another property and split the SqlString on '.', to get the correct alias.
All of this makes perfect sense to me, and the "workaround" is not nice, but I don't see any other way of doing it. NHibernate needs to know, what property it's dealing with, to assign proper aliases. Since I'm not mapping the property, it has no way of knowing, what alias I'm looking for.
I guess I'm doing a lot of hacking, just to avoid having to reference NHibernate.Spatial...

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

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

How to map many columns from one table in database to one array/list in class?

I have a table in database which has some columns like year,name and also 12 columns (m1,m2,...,m12) representing months. I would like to map this table into one class using NHibernate, ideally, these 12 mapped columns would look like:
_mappedMonths[] = new double[12];
Has anyone a solution for this ?
If you really want to map the columns directly to an array, as you describe, take a look at the ICompositeUserType interface. You can find an article about custom NHibernate mapping here, and this blog post might be of interest as well.
However, if it is not super important you might consider mapping the columns just as you normally would, but as private/protected properties, and then create a public property in your class that exposes those private/public properties as an array. That would be a simpler and faster solution, but would result in code that is not quite as clean.

Use type of object in HQL where clause

In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes like FTPChannelSpecification, EMailChannelSpecification and WebserviceChannelSpecification. Now I want to create an HQL query which contains a where clause that narrows down the result to certain types of channel specifications. E.g. (in plain English) select all CommunicationChannelSpecifications that whose types occur in the set {FTPChannelSpecification, WebserviceChannelSpecification}.
How can this be achieved in HQL? I'm using NHibernate 2.0.1 and a table per subclass inheritance mapping strategy...
Thanks!
Pascal
Not positive in NHibernate, but in Hibernate, there are two special properties that are always referenced id and class. So, for your particular case, I'd do
from CommunicationChannelSpecifications spec where spec.class in (?)
NHibernate supports the same syntax as Hibernate in this case. See here for an example.