tags:

views:

62

answers:

1

I need to retrieve all the records in a table with nHibernate. If I had the key for all the records in the table I could loop and use nHibernate's Get method (this seems inefficient though) but I don't have the keys. I could also use FindAll but this requires criteria or a stored procedure.

How can I get all the records from the table?

+3  A: 

SQL tables are mapped to classes so in order to retrieve all records from a table you write a query (HQL or Criteria) that fetches all the objects for a given typed mapped to this table:

var products = session.CreateCriteria<Product>().List<Product>();

or using HQL:

var products = session.CreateQuery("from " + typeof(Product)).List<Product>();

or LINQ:

var products = session.Linq<Product>().ToList() // 2.x contrib provider
var products = session.Query<Product>().ToList() // 3.x integrated provider
Darin Dimitrov
If I understand you correctly, you basically create criteria with no restrictions?
brainimus
exactly, isn't that the requirement?
Jaguar
Yes. I just wanted to make sure I knew what was going on. Thanks!
brainimus