views:

64

answers:

3

I have an interesting scenario where I have a given table that maps to an entity. However, there is another version of the table which gets populated as part of a nightly process and has the exact same columns as the first table. Without getting into too many details, the second table is a "working" table with a small subset of records. There are some weird business rules where inserts can only be done into the large table and reads/updates can only be done on the smaller table.

How could I handle mapping this? In other words, in same cases I want a CRUD operation to use one mapping and in other scenarios I want to use the other table.

+1  A: 

You can do this by either maintaining two session factories with each mapping the same Entity to each table, or by using two diferewnt entities in a single session factory with each entity (class) mapped to it's own table.

Sérgio Costa
+1  A: 

I would handle this as two separate entities and map them separately to the same table. If you're using repositories you could then enforce the rule that the inserts are possible for one entity and selects/updates in the other.

Jamie Ide
A: 

Create two entities and have each mapped to their own separate table. Since the tables are mirrors of each other only use one entity throughout the application. When it comes time to save the entity check an IsNew flag. If IsNew is true then simply save (insert). If IsNew is false then copy the values to the secondary entity and save (update) that. This way you don't have to worry about passing two different entities around in your application. It should keep the confusion down when reading through the code because you don't have to worry about what entity your using.

Greg Bahrey

related questions