ADBlock is blocking some content on the site

ADBlock errore

(N)Hibernate - is it possible to dynamically map multiple tables to the one class

StackOverflow https://stackoverflow.com/questions/51654


I have the situation where i use GIS software which stores the information about GIS objects into separate database table for each type/class of GIS object (road, river, building, sea, ...) and keeps the metadata table in which it stores info about the class name and its DB table.

Those GIS objects of different classes share some parameters, i.e. Description and ID. I'd like to represent all of these different GIS classes with one common C# class (let's call it GisObject), which is enough for what i need to do from the non-GIS part of the application which lists GIS objects of the given GIS class.

The problem for me is how to map those objects using NHibernate to explain to the NHibernate when creating a C# GisObject to receive and use the table name as a parameter which will be read from the meta table (it can be in two steps, i can manually fetch the table name in first step and then pass it down to the NHibernate when pulling GisObject data).

Has anybody dealt with this kind of situation, and can it be done at all?

Solution 2

@Brian Chiasson

Unfortunately, it's not an option to create all classes of GIS data because classes are created dynamically in the application. Every GIS data of the same type should be a class, but my user has the possibility to get new set of data and put it in the database. I can't know in front which classes my user will have in the application. Therefore, the in-front per-class mapping model doesn't work because tomorrow there will be another new database table, and a need to create new class with new mapping.

@all There might be a possibility to write my own custom query in the XML config file of my GisObject class, then in the data access class fetching that query using the

string qs = getSession().getNamedQuery(queryName);

and use the string replace to inject database name (by replacing some placeholder string) which i will pass as a parameter.

qs = qs.replace(":tablename:", tableName);

How do you feel about that solution? I know it might be a security risk in an uncontrolled environment where the table name would be fetched as the user input, but in this case, i have a meta table containing right and valid table names for the GIS data classes which i will read before calling the query for fetching data for the specific class of GIS objects.


It sounds like the simplest thing to do here may be to create an abstract base class with all of the common GIS members and then to inherit the other X classes that will have nothing more than the necessary NHibernate mappings. I would then use the Factory pattern to create the object of the specific type using your metadata.

one way you could do it is to declare an interface say IGisObject that has the common properties declared on the interface. Then implement a concrete class which maps to each table. That way they'll still be all of type IGisObject.

You can have a look at what Ayende is saying here : MultiTable Entities.

But since you have separate tables , i don't think it will work. You can also check out nhuser group

I guess I'd ask the question to why you are going after the GIS data directly in the database and not using what API that is typically provided as an abstraction for you. If this is an ESRI system there are tools that allow you to create static database views into their GIS objects and then maybe from that point it might be appropriate for data extract.

From the NHibernate documentation, you could use one of the inheritance mappings.

You might also have a separate class for each table, but have them all implement some common interface

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow