-->
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: Problem executing query with on auto generated classes
PostPosted: Thu Jan 15, 2009 11:43 pm 
Newbie

Joined: Mon Dec 22, 2008 7:26 pm
Posts: 6
I'm not sure whether this is a hibernate-tools question or just a hibernate question. I haven't experienced this problem when using hibernate without hibernate-tools, though, so I am posting it here.

I am using hibernate tools 3.2.4 along with ant 1.6.5. I am generating classes that inherit from each other by overriding the tableToMetaAttributes method in the DelegatingReverseEngineeringStrategy class, like so.


Code:
public Map tableToMetaAttributes(TableIdentifier tableIdentifier)
   {
      Map attributes = super.tableToMetaAttributes(tableIdentifier);
      
      // Get the map of inheritances between the tables.
      String className = tableIdentifier.getName();
      String superclass = getInheritanceMap().get(className);
      if (superclass == null)
      {
         return attributes;
      }
      
      // If there are no meta attributes yet, initialize the attributes
      // map.
      if (attributes == null)
      {
         attributes = new HashMap<String, MetaAttribute>();
      }
      
      // Add the inheritance to the set of attributes.
      MetaAttribute attribute = new MetaAttribute("extends");
      attribute.addValue(superclass);
      attributes.put("extends", attribute);
      
      return attributes;
   }


I am generating a class called Country that inherits from Location. Here is the country hbm file.
Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 15, 2009 5:38:04 PM by Hibernate Tools 3.2.0.CR1 -->
<hibernate-mapping>
    <class catalog="Enum" name="com.structuralwealth.dsl.Country" table="Country">
        <meta attribute="extends" inherit="false">Location</meta>
        <id name="id" type="java.lang.Long">
            <meta attribute="finder" inherit="false">findById</meta>
            <column name="id"/>
            <generator class="identity"/>
        </id>
        <many-to-one class="com.structuralwealth.dsl.Currency"
            fetch="select" name="currency">
            <meta attribute="finder" inherit="false">findByCurrencyId</meta>
            <column name="currency_id"/>
        </many-to-one>
        <many-to-one class="com.structuralwealth.dsl.Location"
            fetch="select" name="location">
            <meta attribute="finder" inherit="false">findByLocationId</meta>
            <column name="location_id"/>
        </many-to-one>
        <property name="iso2" type="string">
            <meta attribute="finder" inherit="false">findByIso2</meta>
            <column length="2" name="iso2"/>
        </property>
        <property name="iso3" type="string">
            <meta attribute="finder" inherit="false">findByIso3</meta>
            <column length="3" name="iso3"/>
        </property>
        <property name="name" type="string">
            <meta attribute="finder" inherit="false">findByName</meta>
            <column name="name"/>
        </property>
        <property name="isDomestic" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByIsDomestic</meta>
            <column name="is_domestic"/>
        </property>
        <property name="isEmergingMarket" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByIsEmErgingMarket</meta>
            <column name="is_emerging_market"/>
        </property>
        <property name="isBarraSupported" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByIsBarraSupported</meta>
            <column name="is_barra_supported"/>
        </property>
        <property name="isInternationalDeveloped" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByIsInternatIonalDevelopeD</meta>
            <column name="is_international_developed"/>
        </property>
        <property name="isTaxHaven" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByIsTaxHaven</meta>
            <column name="is_tax_haven"/>
        </property>
        <property name="hasForeignWithholdingTax" type="java.lang.Boolean">
            <meta attribute="finder" inherit="false">findByHasForeignWithholdingTax</meta>

            <column name="has_foreign_withholding_tax"/>
        </property>
        <set inverse="true" name="countryPercentRegions">
            <key>
                <column name="country_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CountryPercentRegion"/>
        </set>
        <set inverse="true" name="exchanges">
            <key>
                <column name="country_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.Exchange"/>
        </set>
        <set inverse="true" name="companyRiskEntitysForCountryOfIncorporationId">
            <key>
                <column name="country_of_incorporation_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CompanyRiskEntity"/>
        </set>
        <set inverse="true" name="companyRiskEntitysForCountryOfIssueId">
            <key>
                <column name="country_of_issue_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CompanyRiskEntity"/>
        </set>
        <set inverse="true" name="companyRiskEntitysForIssuerCountryOfDomicileId">
            <key>
                <column name="issuer_country_of_domicile_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CompanyRiskEntity"/>
        </set>
        <set inverse="true" name="barraFactorCountryMaps">
            <key>
                <column name="country_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.BarraFactorCountryMap"/>
        </set>
    </class>
</hibernate-mapping>


I attempt to run the following query

Code:
Session session = sessionFactory.openSession();
      Query query = session.createQuery("FROM Location l WHERE l.rootId = 1");
      query.list();


And I receive the following error.
Code:
Exception in thread "main" org.hibernate.QueryException: could not resolve property: rootId of: com.structuralwealth.dsl.Country [FROM com.structuralwealth.dsl.Country l WHERE l.rootId = 1]
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
   at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1362)
   at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
   at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386)
   at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:567)
   at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
   at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
   at com.structuralwealth.dsl.test.Test.main(Test.java:25)




But that's not the query I executed! what's going on here?

Thanks


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 6:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i don't see rootId being mapped...hence you get an exception about it not being there..

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 2:07 pm 
Newbie

Joined: Mon Dec 22, 2008 7:26 pm
Posts: 6
Indeed, country does not have a rootId field but the my query was for the Location table, not the Country table. Here is the hbm file for Location (sorry, I should have included this originally)

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 16, 2009 9:47:25 AM by Hibernate Tools 3.2.0.CR1 -->
<hibernate-mapping>
    <class catalog="Enum" name="com.structuralwealth.dsl.Location" table="Location">
        <id name="id" type="java.lang.Long">
            <meta attribute="finder" inherit="false">findById</meta>
            <column name="id"/>
            <generator class="identity"/>
        </id>
        <many-to-one class="com.structuralwealth.dsl.LocationLevelEnum"
            fetch="select" name="locationLevelEnum">
            <meta attribute="finder" inherit="false">findByLocationLeveLEnumId</meta>
            <column name="location_level_enum_id"/>
        </many-to-one>
        <property name="code" type="string">
            <meta attribute="finder" inherit="false">findByCode</meta>
            <column length="50" name="code" unique="true"/>
        </property>
        <property name="name" type="string">
            <meta attribute="finder" inherit="false">findByName</meta>
            <column length="75" name="name"/>
        </property>
        <property name="rootId" type="java.lang.Integer">
            <meta attribute="finder" inherit="false">findByRootId</meta>
            <column name="root_id"/>
        </property>
        <property name="lft" type="java.lang.Integer">
            <meta attribute="finder" inherit="false">findByLft</meta>
            <column name="lft"/>
        </property>
        <property name="rgt" type="java.lang.Integer">
            <meta attribute="finder" inherit="false">findByRgt</meta>
            <column name="rgt"/>
        </property>
        <property name="level" type="java.lang.Short">
            <meta attribute="finder" inherit="false">findByLevel</meta>
            <column name="level"/>
        </property>
        <set inverse="true" name="countries">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.Country"/>
        </set>
        <set inverse="true" name="states">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.State"/>
        </set>
        <set inverse="true" name="regions">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.Region"/>
        </set>
        <set inverse="true" name="cities">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.City"/>
        </set>
        <set inverse="true" name="securityTransactionRestrictions">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.SecurityTransactionRestriction"/>
        </set>
        <set inverse="true" name="assetLocationExposureMaps">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.AssetLocationExposureMap"/>
        </set>
        <set inverse="true" name="custodialAccountTradeLocationMaps">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CustodialAccountTradeLocationMap"/>
        </set>
        <set inverse="true" name="custodianTradeLocationMaps">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.CustodianTradeLocationMap"/>
        </set>
        <set inverse="true" name="barraFactorCountryMaps">
            <key>
                <column name="location_id"/>
            </key>
            <one-to-many class="com.structuralwealth.dsl.BarraFactorCountryMap"/>
        </set>
    </class>
</hibernate-mapping>


Even though my query was:

Code:
FROM Location l WHERE l.rootId = 1


It seems from the error message that Hibernate thinks my query was:

Code:
FROM com.structuralwealth.dsl.Country l WHERE l.rootId = 1


And therefore it fails to find the rootId field. That's the behavior that I don't understand. It thinks I'm querying the inheriting table when I'm actually querying the inherited table.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 6:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if Location is said to extend Country then there should be two queries - one for Location another for Country. Look in docs for explicit polymorphism to control this

_________________
Max
Don't forget to rate


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.