hibernate - createCriteria or createAlias?
Asked Answered
E

4

31

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...

Evanevander answered 27/2, 2010 at 12:51 Comment(2)
I'm not sure if this was a Java/Hibernate or a C#/NHibernate question, so have answered for both.Himyarite
there are subtle differences between CreateCriteria and CreateAlias, each has a purpose. Check my answer at #899579 This is actually a duplicate questionKnowing
H
26

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"));
Himyarite answered 27/2, 2010 at 18:22 Comment(4)
sorry didn't tell you that i am using nhibernate for C#. So if i undesrtand correctly createAlias is the same as createCriteria, but createCriteria can use different join but createAlias can only use inner join. Am i right?Evanevander
@Evanevander JoinType parameter is available via both method names.Himyarite
they are not the same thing note that CreateCriteria exposes an ICriteria object which can be manipulated besides the "root" ICriteria object check my answer at #899579Knowing
This answer is simply incorrect. Depending which method you use it has different results: @sorrymissjackson response.Erased
N
22

Adding to xavierzhoa's answer:

There is actually quite a big difference between the two methods which you will notice 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
Nucleon answered 28/4, 2015 at 12:46 Comment(1)
yeah very usefullStoop
I
14

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;
}
Inpour answered 21/1, 2013 at 12:58 Comment(0)
A
0
Criteria criteria = (Criteria)sessionFactory.getCurrentSession().createCriteria(BillEntity.class)
        .createAlias("worksEntity", "worksEntity" ,JoinType.LEFT_OUTER_JOIN)

instead of writing (Jointype) use the import file as

(org.hibernate.sql.JoinType..LEFT_OUTER_JOIN)
Autobus answered 31/8, 2021 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.