-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Criteria not capable of associations?
PostPosted: Thu Apr 14, 2005 5:06 am 
Newbie

Joined: Fri Apr 08, 2005 7:52 am
Posts: 3
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]


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 10:47 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
You have to do the following:

criteria.createAlias("country", "country").add(Restrictions.ilike("country.value",
searchParty.getCountry().getValue(),MatchMode.ANYWHERE));

Hope it helps ;-)


Top
 Profile  
 
 Post subject: Thanks a lot,
PostPosted: Mon May 30, 2005 3:52 am 
Newbie

Joined: Fri Apr 08, 2005 7:52 am
Posts: 3
Your proposed solution works great.

Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.