I have two simple mappings:
<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. Then 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
. I assume both should give the same result. Where I am wrong?
Thanks in advance!
.setProjection(Projections.property("street"))
always operates on the root criteria. To project joined items, you have to use the first version you posted.