0

Good afternoon everyone!

I'm studying NHibernate, and decided to make some changes. Among them, I noticed that some fields are unnecessary. So I bring my doubt: I have a list, let's call it Class_List within each study class, I can have N students for each class. Within the list Class_List, I also have other properties as simple as the name of the class. How I see it is unnecessary to store how many students I have in the database, I would, in a single query, how many records I have. This, using NHibernate.

Is this possible? How?

Best regards,

Gustavo.

Edit: I've forgot to say one thing... I want to return this number of record, as a column. But this column is not mapped in my .hbm.xml file.

Gustavo Gonçalves
  • 528
  • 1
  • 12
  • 33

1 Answers1

1

If students are mapped as a collection on Class, you can try using something like this:

var numberOfStudents = session.CreateCriteria<Class>()
    .Add(Restrictions.IdEq(1))
        .CreateCriteria("_students", "students")
        .SetProjection(Projections.RowCount())
        .UniqueResult<Int32>();

Where '1' is the id of the class (you can use other property) and '_students' is the name of the students collection.

Dmitry
  • 17,078
  • 2
  • 44
  • 70