hibernate - createCriteria or createAlias?

If I want to search those students who take class "Math" and "John" is his group:

shoud I use createCriteria or createAlias?

Criteria:

Criteria criteria = session.createCriteria(Student.class);
Criteria subquery1 = criteria.createCriteria("courses", course).add(Restrictions.eq(course.name, "Math"));
Criteria subquery2 = criteria.createCriteria("group", student).add(Restrictions.eq(student.name, "John"));

how to put subquery1 and subquery2 together with initial criteria?

Alias:

Criteria criteria = session.createCriteria(Student.class).
createAlias("courses", course).add(Restrictions.eq(course.name, "Math")).
createCriteria("group", student).add(Restrictions.eq(student.name, "John"));

When to use createCriteria and when createAlias? I think the boath are the same...

Answers


CreateAlias and CreateCriteria are identical in the current versions of Hibernate and NHibernate. The only difference being that CreateCriteria has 2 additional overloads without the alias parameter.

Presumably they were different in a older version, but any differences are long gone.

An alias can be defined in terms of another alias, so your first example can be written as:

// Java
Criteria criteria = session.createCriteria(Student.class)
    .createAlias("courses", "course")
    .createAlias("course.group", "student")
    .add(Restrictions.eq("course.name", "Math"))
    .add(Restrictions.eq("student.name", "John"));

// C#
ICriteria criteria = session.CreateCriteria<Student>()
    .CreateAlias("Courses", "course")
    .CreateAlias("course.Group", "student")
    .Add(Restrictions.Eq("course.Name", "Math"))
    .Add(Restrictions.Eq("student.Name", "John"));

Please refer to the following source code from the Hibernate

public Criteria createCriteria(String associationPath, String alias, int joinType) {
    return new Subcriteria( this, associationPath, alias, joinType );
}


public Criteria createAlias(String associationPath, String alias, int joinType) {
    new Subcriteria( this, associationPath, alias, joinType );
    return this;
}

Adding to xavierzhoa's answer:

There is actually quite a big difference between the two methods which will make a difference if you chain Criteria methods. You will continue to work on the original Criteria object when using createAlias, whereas you work on a more nested scope when using createCriteria.

Consider this:

    Criteria c = getSession()
      .createCriteria(YourEntity.class)
      .createCriteria("someMember", "s")
      .add(Restrictions.eq("name", someArgument));  // checks YourEntity.someMember.name

versus

    Criteria c = getSession()
      .createCriteria(YourEntity.class)
      .createAlias("someMember", "s")
      .add(Restrictions.eq("name", someArgument));  // checks  YourEntity.name

However, if you always assign and use an alias you will be able to work around the difference. Like:

    Criteria c = getSession()
      .createCriteria(YourEntity.class, "y")
      .createAlias("someMember", "s")
      .add(Restrictions.eq("y.name", someArgument));  // no more confusion

Need Your Help

Merging NSArrays containing NSDictionarys

ios objective-c merge nsmutablearray nsmutabledictionary

I have an NSMutableArray of NSDictionarys something like this:

sendmail: OpenSSL::SSL::SSLError: hostname was not match

ruby-on-rails ruby-on-rails-3 openssl sendmail actionmailer

I have a vps with ubuntu 10.04. i'm tring to install a rails 3 application

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.