tags:

views:

25

answers:

1

I have a class called Continent and Country.

Continent class has Country collection:

    private ISet<Country> _countries;
    public virtual ISet<Country> Countries
    {
        get { return _countries; }
        set { _countries = value; }
    }

I want to write an HQL query that will get all the continents that have countries at least one country with CountryName = "A"

My country class has property:

    public virtual string CountryName { get; set; }

And I have my .hbm.xml files have the Db relation information for both the Continent and Country objects.

How should I write my HQL here:

public IList<Continent> GetContinentsWithCountriesStartingWithLetter(char letter)
        {

            string query = ":letter"; //The query to be used
            return
                _session
                    .CreateQuery(query)
                    .SetString("letter", letter.ToString())
                    .List<Continent>();
        }

Thanks!

+2  A: 
return _session
    .CreateQuery("from continent in Continent inner join continent.Countries country where country.Name like :letter")
    .SetString("letter", letter)
    .List<Continent>();

HTH,
Kent

Kent Boogaart
@Kent Boogart: Thanks! Can you recommend me a good resource covering all the HQL syntax and usage, please?
burak ozdogan
@burak: I wouldn't say it's good, but have you got the NH user guide handy? You should grab the PDF and have it lying about wherever you do NH dev.
Kent Boogaart
@ Kent Boogart: I assume you mean this: http://docs.jboss.org/hibernate/core/3.3/reference/en/pdf/hibernate_reference.pdf Thank you Kent!
burak ozdogan
@burak: there are NH-specific ones on SourceForge: http://sourceforge.net/projects/nhibernate/files/NHibernate/. To be honest, you still see Java remnants in the NH docs, but you're still better off with the NH ones I think - will probably help avoid some confusion.
Kent Boogaart