Hibernate Verein mit createCriteria und createAlias

Habe ich zwei einfache 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>

und

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

Ich versuche zu bekommen-Eigenschaft der person-Adresse wie folgt:

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

und es funktioniert.
Als wie diese:

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

und es wirft: org.hibernate.QueryException: could not resolve property: street of: org.testing.Person. Ich nehme an, beide sollten das gleiche Ergebnis. Wo bin ich falsch?
Vielen Dank im Voraus!

Warum sind Sie mit createCriteria("personAddress") in der zweiten Abfrage?
Ich benutze es, weil es eine Eigenschaft namens personAddress in Person, Klasse und es ist ein Ende der Assoziation der Adresse Klasse

InformationsquelleAutor catdog | 2012-01-03

Schreibe einen Kommentar