Hi,
I am trying to implement an enhanced "query by example" functionality (the included QBE class does not work for the query want to do). I use the Criteria class to build up a list of restrictions my result has to fulfill. This works perfectly if I only query direct attributes of the queried class (in the example below, Party.name, Party.street, ...), but gives me a
Code:
org.hibernate.QueryException
could not resolve property: country.value of: Party
exception when I try to query for an attribute of a joined class (in this case, the CountryCode class, which has a many-to-one relationship to my Party class. Details about the mapping documents and code used to trigger the exception can be found below.
Does the Criteria class use HQL to create the SQL queries, or does it directly create SQL? I think in HQL it should be possible to do a query like:
Code:
from Party where country.value='AT'
while in SQL you would have to explicitly create a join. So if HQL is used in between, I think my Criteria example below should work.
Any thoughts on this?
Best Regards,
Christoph Jäger
Hibernate version: Hibernate 3.0
Mapping documents:Code:
<hibernate-mapping package="at.cargodata.cargosync">
<class name="Party" table="party">
<id name="id" column="id" unsaved-value = "null">
<generator class="sequence"><param name="sequence">party_seq</param></generator>
</id>
<property name="name"/>
<property name="street"/>
<property name="zip"/>
<property name="city"/>
<many-to-one name="country" column="country_id" class="CountryCode" not-null="true"/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping package="at.cargodata.cargosync.systemcode">
<class name="SystemCode" table="systemcode" discriminator-value="XXXX" lazy="false">
<id name="id" column="id" unsaved-value="null">
<generator class="sequence">
<param name="sequence">systemcode_seq</param>
</generator>
</id>
<discriminator column="code" type="string"/>
<property name="value"/>
<property name="description"/>
<property name="descriptionLong"/>
<subclass name="SystemCodeType" discriminator-value="syco"/>
<subclass name="CountryCode" discriminator-value="coco"/>
<subclass name="PackingType" discriminator-value="pack"/>
<subclass name="Currency" discriminator-value="curr"/>
<subclass name="ContactFunction" discriminator-value="cofu"/>
</class>
</hibernate-mapping>
The code I am trying to execute looks like follows:
Code:
...
Criteria criteria=session.createCriteria(Party.class);
criteria.setFetchMode("country", FetchMode.JOIN);
// special condition: name has to be longer than 5 chars
criteria.add(Restrictions.sqlRestriction("length(name)>5"));
if (searchParty==null) return criteria;
criteria.add(Restrictions.ilike("name",searchParty.getName(),MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("street",searchParty.getStreet(),MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("zip",searchParty.getZip(),MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("city",searchParty.getCity(),MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("country.value",
searchParty.getCountry().getValue(),MatchMode.ANYWHERE));
return criteria;
}
Name and version of the database you are using: PostgreSQL 7.4
[/code]