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