-->
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.  [ 4 posts ] 
Author Message
 Post subject: newbie: Criteria Query Generating Exception :-(
PostPosted: Mon Jul 25, 2005 6:40 am 
Newbie

Joined: Mon Jul 25, 2005 6:24 am
Posts: 3
Location: India
Hi everyone,

I am trying to represent a geo-hierarchy in a database. I am using Hibernate v2.18.

The hierarchy is a typical parent-child relationship.

Country
|
|__State

The mapping document for Country is:

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

<hibernate-mapping
   package="com.abc.def.persistence">
   <class
      name="Country" table="COUNTRY">
      <id name="countryID" column="COUNTRY_ID" type="java.lang.Long">
         <generator class="native"/>
      </id>
      <property
         name="label"
         type="string"
         length="40"
         not-null="true"/>
      <set
         name="states"
         inverse="true"
         cascade="all-delete-orphan">
         <key column="COUNTRY_ID"/>
         <one-to-many class="State"/>
      </set>
   </class>
</hibernate-mapping>


Now when I am trying to find a country using the following criteria query

Code:
final Country parent = (Country)session.createCriteria( Country.class )
         .add( Expression.eq("COUNTRY_ID", parentEntityID) )
         .uniqueResult();


where parentEntityID has type java.lang.Long.

I am getting the exception:

Code:
net.sf.hibernate.QueryException: could not resolve property: COUNTRY_ID of: com.abc.def.persistence.Country
   at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:50)
   at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:42)
   at net.sf.hibernate.expression.SimpleExpression.toSqlString(SimpleExpression.java:40)
   at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:64)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3642)
   at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238)
   at net.sf.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:385)
   at com.abc.def.services.GeographicDataService.addEntity(GeographicDataService.java:73)
   at com.abc.def.loader.GeographicDataLoader.loadData(GeographicDataLoader.java:57)
   at com.abc.def.test.Tester.main(Tester.java:13)


Can somebody help me understand, what's wrong ??

The POJO Code for Country is:

Code:
package com.abc.def.persistence;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;


public class Country implements Serializable
{
   private static final long serialVersionUID = 1L;

   private Set states;
   private Long countryID;
   
   private String label;
   
   public Country()
   {
      // left intentionally blank
      // to be used by Hibernate
   }
   
   public Country( final String label )
   {
      this.label    = label;
      this.states = new HashSet();
   }
   
   public Long getCountryID()
   {
      return countryID;
   }
   
   protected void setCountryID( final Long countryID )
   {
      this.countryID = countryID;
   }
   
   public Set getStates()
   {
      return states;
   }
   
   public void setStates( final Set states )
   {
      this.states = states;
   }
   
   public void addState( final State state )
   {
      if( state == null )
      {
         throw new IllegalArgumentException("State reference is null.");
      }
      
      final Country country = state.getCountry(); 
      
      if( country != null )
      {
         country.getStates().remove( state );
      }
         
      state.setCountry( this );
      
      states.add( state );
   }
   
   public String getLabel()
   {
      return label;
   }
   
   public void setLabel( final String label )
   {
      this.label = label;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 6:57 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
try it with
Code:
final Country parent = (Country)session.createCriteria( Country.class )
         .add( Expression.eq("countryID", parentEntityID) )
         .uniqueResult();


you have to specifiy the property- not the column-name.

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 7:20 am 
Newbie

Joined: Mon Jul 25, 2005 6:24 am
Posts: 3
Location: India
Thanks curio,

It worked... thanks a lot for your help...

Their is one more thing that's making me concerned.... the Hibernate SQL is generated as:

Code:
Hibernate: insert into COUNTRY (label) values (?)

Hibernate: select this.COUNTRY_ID as COUNTRY_ID0_, this.label as label0_ from COUNTRY this where this.COUNTRY_ID=?
Hibernate: insert into STATE (label, COUNTRY_ID) [b]values (?, ?)[/b]



Why ?, ? is appearing in the values. Is this the expected behavior.

Also, Hibernate is using outer-join... Is this right...??

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 7:56 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
no problem ;)

yes, the "?" are all right. Hibernate is using PreparedStatements. If you set your log-level to "debug" you will see the values, hibernate is setting for the "?".

outer join is all right, too ...

gtx
curio


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