Hibernate association with createCriteria and createAlias

I have two simple mappings:

<class name="org.testing.Person" table="PERSON">
        <id name="personId" type="long" column="PERSON_ID">
            <generator class="native"/>
        <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 name="org.testing.Address" table="ADDRESS">
    <id name="addressId" type="long" column="ADDRESS_ID">
        <generator class="native" />
    <property name="street" column="ADDRESS_STREET" type="string" />
    <property name="city" column="ADDRESS_CITY" type="string" />
    <property name="state" column="ADDRESS_STATE" type="string" />

I try to get property of the person address like this:

                    .add(Restrictions.eq("personName", "Frodo"))
                    .createAlias("personAddress", "pa")

and it works. Then like this:

                    .add(Restrictions.eq("personName", "Frodo"))

and it throws: 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!


  • .setProjection("street")) always operates on the root criteria. To project joined items, you have to use the first version you posted.