views:

34

answers:

2

I got a legacy database which have about 10 identical tables (only name differs). Is it possible to be able to use the same business entity for all tables without having to create several classes/mapping files?

+1  A: 

You can create a base class with all the properties, but you still need to map them all.

For that, you can either use copy&paste, XML entities (see examle at http://nhforge.org/doc/nh/en/index.html#inheritance-tableperconcreate-polymorphism), or a code-based mapping method (Fluent or ConfORM). They usually make reuse easier.

Diego Mijelshon
Upvoted since it would work, although the answer by Sisyphus is more close to what I need. I'm using the same repository for all objects (treating them as the same type, but using the table name to identity them)
jgauffin
+1  A: 

You can use the entity-name feature if you are using NHibernate v2.1 or higher. It is poorly documented but I am actively using the feature. It has gotten hard to find the documentation on it but look here:

Section 5.3 in

http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname

A couple of things to be aware of. You must now use entity-name instead of class name to refer to the objects. In general it is not an entirely transparent change moving from class names to entity names.

Session actions now require two parameters, for example:

_session.Save("MyEntity", myobject)

The entity-name controls what table the data goes into.

Some HQL queries do not work right anymore, sometimes you must use Criteria instead.

If you need a set of sample code I may be able post some, but far too busy at the moment. I suggest you look at the limited info you can find and set it up for a very simple object and multiple tables to learn how it all works. It does work.

Sisyphus

related questions