-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria problem
PostPosted: Wed Sep 27, 2006 5:45 am 
Newbie

Joined: Thu Aug 31, 2006 7:56 am
Posts: 14
I am having difficulty getting off the ground with Criteria. I have the following HQL statement which works fine, and I am trying to build tyhe corresponding Criteria version.

Code:
from Order ord left join fetch ord.Rep  where ord.Rep.RepGroup.Code > :code.


1st try:
Code:
    Criteria criteria = session.createCriteria(Order.class);
    criteria  = criteria.setFetchMode("Rep", FetchMode.JOIN);
    criteria = criteria.add(Expression.ge("Order.Rep.RepGroup.Code", repCode));

I get the error

Code:
org.hibernate.QueryException: could not resolve property: Order of: com.myapp.Order



2nd try
Code:
    Criteria criteria = session.createCriteria(Order.class);
    criteria  = criteria.setFetchMode("Rep", FetchMode.JOIN);
    criteria = criteria.add(Expression.ge("Rep.RepGroup.Code", repCode));


I got the error:
Code:
org.hibernate.QueryException: could not resolve property: Rep.RepGroup.Code of: com.myapp.Order


3rd try
Code:
    Criteria criteria = session.createCriteria(Order.class);
    criteria  = criteria.setFetchMode("Rep", FetchMode.JOIN);
    criteria = criteria.add(Expression.ge("RepGroup.Code", repCode));


I got the error:
Code:
org.hibernate.QueryException: could not resolve property: RepGroup of: com.mypp.Order


Any assistance much apprectaied.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 6:08 am 
Newbie

Joined: Mon Aug 28, 2006 8:13 am
Posts: 6
Hi

please post your mapping files.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 6:39 am 
Newbie

Joined: Thu Aug 31, 2006 7:56 am
Posts: 14
ansi_c wrote:
Hi

please post your mapping files.

thanks

Apologies, I did not include the mapping files cos it was working using hql, here they are..

Code:
Mapping files, Order, Rep & RepGroup

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.myco">
   <class
      [b]name="Order"[/b]
      table="Order"
   >
      <meta attribute="sync-DAO">false</meta>


      <id
         name="Id"
         type="java.lang.Long"
         column="id"
         unsaved-value="null"
      >
         <generator class="increment"/>
      </id>

      <!-- please tell Joe Hudson that the type 'nchar' could not be resolved.. defaulting to java.lang.String -->
      <property
         name="Order"
         column="order"
         type="java.lang.String"
         not-null="true"
         length="20"
      />

      ..
      ..
       
      <many-to-one
         name="Rep"
         column="repId"
         class="Rep"
         not-null="true"
      >
      </many-to-one>
   </class>   
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.myapp">
   <class
      [b]name="Rep"[/b]
      table="Rep"
   >
      <meta attribute="sync-DAO">false</meta>
      <id
         name="Id"
         type="integer"
         column="id"
      >
         <generator class="increment"/>
      </id>

      <property
         name="Code"
         column="code"
         type="string"
         not-null="true"
         length="10"
      />
..
..
      <many-to-one
         name="RepGroup"
         column="repGroupId"
         class="RepGroup"
         not-null="true"
      >
      </many-to-one>
   

      <set name="Orders" inverse="true">
         <key column="id"/>
         <one-to-many class="Order"/>
      </set>


   </class>   
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.myapp">
   <class
      [b]name="RepGroup"[/b]
      table="RepGroup"
   >
      <meta attribute="sync-DAO">false</meta>
      <id
         name="Id"
         type="integer"
         column="id"
      >
         <generator class="increment"/>
      </id>

      <property
         name="Code"
         column="code"
         type="string"
         not-null="true"
         length="10"
      />
      <property
         name="Name"
         column="name"
         type="string"
         not-null="true"
         length="50"
      />
   

      <set name="Reps" inverse="true">
         <key column="id"/>
         <one-to-many class="Rep"/>
      </set>


   </class>   
</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 10:03 am 
Newbie

Joined: Mon Aug 28, 2006 8:13 am
Posts: 6
Hi

in my opinion a possible solution should look like this:

Code:
Criteria criteria = session.createCriteria(Order.class);
criteria.createCriteria("Rep");
criteria.createCriteria("RepGroup");
criteria.add(Expression.ge("Code", repCode));


Please notice that this code isn't tested so far.

You are able to walk througth the table relations by adding an new Criteria on the property name which hold the for example id of the related table.

In future please start porperties with lower charakters otherwise it is a bit confusing.

sry for my english ...

best regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 7:49 am 
Newbie

Joined: Thu Aug 31, 2006 7:56 am
Posts: 14
ansi_c wrote:

In future please start porperties with lower charakters otherwise it is a bit confusing.


Thank you my friend for assistance, I will try that now. Regarding the case of the properties, I am using Eclipse Hibernate plugin & this creates the property names in uppercase (not camelCase), and unless I conform to this, my HQL will not work...... I found this out the hard way.

Thanks again for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.