Same table with different name Mapping NHibernate - nhibernate-mapping

I have multiple table in database with exactly same schema but different table name.
table_1
{
col1,
col2,
col3,
}
.
.
table_2
{
col1,
col2,
col3,
}
I wanted to keep single mapping HBM file and model class. I tried creating different Naming strategy but the problem is to pass table name from Repository class and perform DB actions. Solution of this problem is much appreciated since I am new to NHibernate.

Do you want to map these tables to the same object or different objects?
Honestly, I do not think you can do it, HBM files are stored as resources in the DLL and can not be edited dynamically.

Related

how to use blob pattern as an alternative of EAV

In the literature and on the forums people often say that EAV is evil and often sugest using Serialized LOB pattern as an alternative of EAV, but don't say something concrete how to use it.
I wonder how to overcome problems with using BLOB pattern as an alternative of EAV.
Let’s assume that we could store all custom fields of the entity in a field custom_fields as a string for example in JSON something like tihis:
{customField1: value1, customField2: value2, …,
customFieldN: valueN}
Let's assume the table subscribers has fields:
id, email, custom_fields (where all custom fields is stored)
How to overcome the following problems:
1. How to seach by seperate custom fields, for example, to find entities with the conditions custField1 = value1 AND customField2 = value2?
2. How to mantain data integrity, for example, if we delete a custom field for the entity how to delete all values oif these custom fields in the entity?

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.

Map dynamic/generic properties with NHibernate

I have a scenario where I have a propertybag of key/values that would look something like:
Entry 1 : key="Load", value="2", type="int"
Entry 2 : key="DailyStatus", value="0", type="bool"
I am trying to figure out if it's possible with nhibernate to map these values to a single table that I can pull out at a later time into .net simple types.
I am trying to avoid creating classes to contain all of this data as it can be very repetitive and doesn't allow portions of the application to be as flexible as possible. I had considered storing it in XML or JSON, but this data has to be queried against on a pretty regular basis.
Has anyone mapped dictionaries of simple types to a table in nhibernate and pulled the data back out? I suppose mapping to a generic dictionary would work:
IDictionary<string, IDictionary<object, Type>>
I can do it by hand, but if there is a builtin way for nhibernate to accomplish it that would be easier.
How about you create a class "Triplet" with attriutes id, key, value, type and then map it to a table called whatever you want?

Conditional Relations in NHibernate

I have a table that needs relations to 2 tables, according to ObjectType column.
For example if ObjectType=1 then column Object should point to TABLE1, and if ObjectType=2 then point to TABLE2.
Can I accomplish this in NHibernate mappings or as Fluent NHibernate?
If not will you suggest me using same Interfaces for both Table classes? (Note: table schemas are totally different)
Why not reference both tables, and use one or the other according to your needs in the class code?
Use a property that returns a common interface for both tables and gives one table or the other according to the object type.

Lazy loading a portion of a record with NHibernate

I'm not sure how to explain this. So here goes...
I'm trying to fit the method for lazy loading blobs as described here but I'm stuck with only one table.
I have a schema (fixed, in a legacy system) which looks something like this:
MyTable
ID int
Name char(50)
image byte
This is on Informix, and the byte column is a simple large object. Now normally I would query the table with "SELECT ID, Name, (image is not null) as imageexists..." and handle the blob load later.
I can construct my object model to have two different classes (and thus two different map definitions) to handle the relationship, but how can I "fool" nhibernate into using the same table to show this one-to-one relationship?
Short answer: you can't.
You either need to map it twice or (my preference) create a DTO that has the fields you want. In HQL you'd do something like:
select new MyTableDTO(t.ID, t.name) from MyTable t