Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve all records in a table with nHibernate

Tags:

nhibernate

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?

like image 300
brainimus Avatar asked Jun 16 '10 13:06

brainimus


1 Answers

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
like image 137
Darin Dimitrov Avatar answered Oct 02 '22 12:10

Darin Dimitrov