简体   繁体   English

与createCriteria和createAlias的休眠关联

[英]Hibernate association with createCriteria and createAlias

I have two simple mapings: 我有两个简单的映射:

<class name="org.testing.Person" table="PERSON">
        <id name="personId" type="long" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <property name="personName" type="string" not-null="true" column="PERSON_NAME"/>
        <many-to-one name="personAddress" class="org.testing.Address" column="PERSON_ADDRESS"    not-null="true" cascade="all" unique="true"/>
</class>

and

<class name="org.testing.Address" table="ADDRESS">
    <id name="addressId" type="long" column="ADDRESS_ID">
        <generator class="native" />
    </id>
    <property name="street" column="ADDRESS_STREET" type="string" />
    <property name="city" column="ADDRESS_CITY" type="string" />
    <property name="state" column="ADDRESS_STATE" type="string" />
</class>

I try to get property of the person address like this: 我试图得到这样的人地址的财产:

session.createCriteria(Person.class)
                    .add(Restrictions.eq("personName", "Frodo"))
                    .createAlias("personAddress", "pa")
                    .setProjection(Projections.property("pa.street"))
                    .list();

and it works. 而且有效。 Than like this: 比这样:

session.createCriteria(Person.class)
                    .add(Restrictions.eq("personName", "Frodo"))
                    .createCriteria("personAddress")
                    .setProjection(Projections.property("street"))
                    .list();

and it throws: org.hibernate.QueryException: could not resolve property: street of: org.testing.Person . 并抛出: org.hibernate.QueryException: could not resolve property: street of: org.testing.Person I assume both should give the same result. 我认为两者应该给出相同的结果。 Where I am wrong? 我哪里错了? Thanks in advance! 提前致谢!

hibernate 3.3.2; 休眠3.3.2;

let us see code source : 让我们看看代码源:

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


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

createCriteria return sub Criteria ,so all the operation is on sub Criteria ; createCriteria返回sub Criteria,因此所有操作都在sub Criteria上; but createAlias is return the criteria who create sub Criteria ; 但是createAlias返回创建sub Criteria的条件;

example 1. Ad manyToone adgroup ; 示例1. Ad manyToone广告组; all of them have " name "properties 它们都有“名称”属性

     Criteria criteria1    = s.createCriteria(Ad.class, "ad").add(Restrictions.eq("ad.id", Long.valueOf(343909))) ;
     Criteria criteria2    =  criteria1.createCriteria("adGroup")
                    .setProjection(Projections.property("name"))
         List list  = criteria2.list();


    List list = s.createCriteria(Ad.class, "ad").add(Restrictions.eq("ad.id", Long.valueOf(343909)))
                    .createAlias("adGroup", "adGroup")
                    .setProjection(Projections.property("name"))
                    .list();

let us say the name in adgroup is " aaaa " the name in ad is "bbbb" in first statement , "name" value of result is from adGroup entity = " aaaa ". 假设广告组中的名称为“ aaaa”,第一条语句中广告中的名称为“ bbbb”,结果的“名称”值来自adGroup实体=“ aaaa”。 in second statement , "name" value of result is from Ad entity "bbbb" . 在第二个语句中,结果的“名称”值来自广告实体“ bbbb”。

ps1: if there is no "name" properties in adgroup entity ,but in ad entity, first statement will throw Exception: org.hibernate.QueryException: could not resolve property: name of: AdGroup ; ps1:如果广告组实体中没有“名称”属性,但广告实体中,则第一条语句将引发异常:org.hibernate.QueryException:无法解析属性:名称:AdGroup; but if u change to " .setProjection(Projections.property("ad.name")) ",it will be fine returning the value of ad's name 但如果您将其更改为“ .setProjection(Projections.property(” ad.name“))”,则可以很好地返回广告名称的值

ps2 :first statement call list() by criteria2 ,but hibernate will process from the root criteria = criteria1 ; ps2:按criteria2进行的第一条语句调用list(),但是休眠将从根criteria = criteria1进行处理; so the final sql will contain " where ad.id =343909 " from criteria1 ; 因此最终的sql将包含条件1中的“ where ad.id = 343909”;

example 2. 范例2。

在此处输入图片说明

teacher oneToMany student student oneToMany student 一对一的学生一对一的学生

  List list1 = s.createCriteria(Teacher.class, "teacher").add(Restrictions.eq("teacher.id", Long.valueOf(343909)))
                    .createCriteria("students").createCriteria("books")
                    .list();



 List list2 = s.createCriteria(Teacher.class, "teacher").add(Restrictions.eq("teacher.id", Long.valueOf(343909)))
                    .createAlias("students").createCriteria("books").setProjection(Projections.property("name"))
                    .list();

second statement will throw Exception: org.hibernate.QueryException: could not resolve property: books of: Teacher; 第二条语句将抛出异常:org.hibernate.QueryException:无法解析属性:书籍:教师; because books is a property of student that is difference between createCriteria and createAlias ; 因为书籍是学生的财产,所以createCriteria和createAlias是不同的;

.setProjection(Projections.property("street")) always operates on the root criteria. .setProjection(Projections.property("street"))始终根据根条件进行操作。 to project joined items you have to use the first version you posted. 要投影联接的项目,您必须使用发布的第一个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM