0
votes

I'm using NHibernate on my project and have implemented auditing functionality using event listeners.

I now need to get this data back, I want to return it exactly as it is in the database I dont need a rich object model with relationships all I want is the IDs of any relationships.

I am basically just trying to show an audit log (which is essentially a copy of what you would see if you opened the table up in sql).

I need to be able to do this for many tables though and all I have is the audit table name, is there a generic way to get NHibernate to query a table and just return the results as a datatable or something simple like that that doesn't have a defined model?

So essentially I want NHibernate to do this:

SELECT * FROM "tablename"

and then return it in some generic way that I can put in a grid and have it autogenerate the columns off it.

2

2 Answers

0
votes

You could use the following to query unmapped objects.

string tablename = "nonmapped";
IList<Unmapped> result = session
                                .CreateSqlQuery("select Id, Description from " + tablename)
                                .SetResultTransformer(Transformer.AliasToBean(typeof(Unmapped)))
                                .List<Unmapped>();

More: http://lostechies.com/jimmybogard/2010/06/30/ad-hoc-mapping-with-nhibernate/

0
votes

you can use the 'select' method to get only the fields you want:

session.CreateCriteria(typeof(Cat)).Select(c=> new {Id = c.Id, MateId = c.Mate.Id});