views:

348

answers:

2

Say I'm mapping a simple object to a table that contains duplicate records and I want to allow duplicates in my code. I don't need to update/insert/delete on this table, only display the records.

Is there a way that I can put a fake (generated) ID column in my mapping file to trick NHibernate into thinking the rows are unique? Creating a composite key won't work because there could be duplicates across all of the columns.

If this isn't possible, what is the best way to get around this issue?

Thanks!

Edit: Query seemed to be the way to go

A: 

The NHibernate mapping makes the assumption that you're going to want to save changes, hence the requirement for an ID of some kind.

If you're allowed to modify the table, you could add an identity column (SQL Server naming - your database may differ) to autogenerate unique Ids - existing code should be unaffected.

If you're allowed to add to the database, but not to the table, you could try defining a view that includes a RowNumber synthetic (calculated) column, and using that as the data source to load from. Depending on your database vendor (and the products handling of views and indexes) this may face some performance issues.

The other alternative, which I've not tried, would be to map your class to a SQL query instead of a table. IIRC, NHibernate supports having named SQL queries in the mapping file, and you can use those as the "data source" instead of a table or view.

Bevan
A: 

If you're data is read only one simple way we found was to wrapper the query in a view and build the entity off the view, and add a newguid() column, result is something like

SELECT NEWGUID() as ID, * FROM TABLE

ID then becomes your uniquer primary key. As stated above this is only useful for read-only views. As the ID has no relevance after the query.

Jafin