How to perform a HQL query in C#?

Asked

Viewed 336 times

1

I need to create an HQL query within a method in C#. The structure goes like this: I have the method:

IList<int>GetListYear(Guid educationalInstitutionId, Guid academicLevelId, Guid? locationId, Guid? programOfferedId)
{
    //Implementar a consulta HQL
} 

And I have my HQL appointment:

"select distinct ConclusionYear
    from AlumniProgramOffered
    inner join AlumniSignup
    inner join ProgramOffered
    inner join Program
    where AlumniSignup.EducationalInstitution.Identity = educationalInstitutionId
and Program.AcademicLevel.Identity = academicLevelId
and ProgramOffered.Location.Identity = locationId or locationId is null
and ProgramOffered.Identity = programOfferedId or programOfferedId is null"

How can I assemble this structure to join the HQL query with the parameters I received from my method?

1 answer

3

Guy would look something like this:

IList<int> GetListYear(Guid educationalInstitutionId, Guid academicLevelId, Guid? locationId, Guid? programOfferedId)
{
    StringBuilder hql = new StringBuilder();

    hql.Append(" select distinct ConclusionYear ");
    hql.Append(" from AlumniProgramOffered ");
    hql.Append(" inner join AlumniSignup ");
    hql.Append(" inner join ProgramOffered ");
    hql.Append(" inner join Program ");
    hql.Append(" where AlumniSignup.EducationalInstitution.Identity = :educationalInstitutionId ");
    hql.Append(" and Program.AcademicLevel.Identity = :academicLevelId ");
    hql.Append(" and ProgramOffered.Location.Identity = :locationId or locationId is null ");
    hql.Append(" and ProgramOffered.Identity = :programOfferedId or programOfferedId is null" ");

    Query query = Session.CreateHqlQuery(hql.ToString());
    query.SetParameter("educationalInstitutionId", educationalInstitutionId);
    query.SetParameter("academicLevelId", academicLevelId);
    query.SetParameter("locationId", locationId);
    query.SetParameter("programOfferedId", programOfferedId);

    return query.ToList<int>();
} 
  • Hello @Thiagofalcão. I tried to use Query query = Session.CreateHqlQuery(hql.ToString()); but does not recognize and asks to make the reference. I made the reference with NHibernate.Engine, which was suggested but did not work.

  • right, try to change the part that creates the query to this: Query query = Session.createQuery(hql.Tostring()); if something is incorrect, try to put the first uppercase letter of Session and createQuery. the reference site is out of stock :( https://docs.jboss.org/hibernate/orm/3.3/reference/pt-BR/html/queryhql.html

  • It didn’t work @Thiagofalcão. He doesn’t recognize the class Query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.