-->
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: HQL against collection of composite elements
PostPosted: Thu Dec 11, 2003 8:20 pm 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
I'm having trouble working out how to write a query where I need to select against a value in a collection of composite elements.

This is the mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="ourcommunity.article.dao.ArticleDO" table="Article">
        <cache usage="read-write" />
   
        <id name="id" type="int" column="ID" unsaved-value="0" >
            <generator class="identity"/>
        </id>
       
        <version name="version" type="long" />

        <property name="enabled" />
       
        <set name="categories" table="ArticleCategory" cascade="all">
            <cache usage="read-write" />
            <key column="OrgID"/>
            <composite-element class="ourcommunity.category.dao.WeightedCategoryDO">
                <property name="weight" />
                <property name="categoryId" />
            </composite-element>
        </set>         
    </class>
</hibernate-mapping>


and this is the query I tried:
Code:
SELECT
  a
FROM
  ourcommunity.article.dao.ArticleDO a,
  ourcommunity.category.dao.WeightedCategoryDO ac
WHERE
  a.enabled=1
AND ac in elements(a.categories)
AND ac.categoryId=?


But when I run the query to SQL get sent to the DB and this message in the logs:
Code:
WARN  [QueryTranslator] no persistent classes found for query class: SELECT a FROM ourcommunity.article.dao.ArticleDO a, ourcommunity.category.dao.WeightedCategoryDO ac WHERE a.enabled=1 AND ac in elements(a.categories) AND ac.categoryId=?


What is the correct way to do this query in HQL? Or do I have to do an SQL query?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 7:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Use Hibernate 2.1, component cannot be queried in 2.0.x
You cannot request directly a composite element (can't be in the from clause)
Try to join with another ArticleDO

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 7:18 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Try
Code:
SELECT
  a
FROM
  ourcommunity.article.dao.ArticleDO a
WHERE
  a.enabled=1
AND a.categories.categoryId=?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 14, 2003 5:18 pm 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
I'm using Hibernate 2.1 rc1 and this does not work... I get the folowing exception:

Code:
net.sf.hibernate.QueryException: expecting 'elements' or 'indices' after: categoryId [SELECT a FROM ourcommunity.article.dao.ArticleDO a WHERE a.enabled=1 AND a.categories.categoryId=?]
        at net.sf.hibernate.collection.CollectionPropertyMapping.toType(CollectionPropertyMapping.java:53)
        at net.sf.hibernate.hql.PathExpressionParser.getPropertyType(PathExpressionParser.java:244)
        at net.sf.hibernate.hql.PathExpressionParser.end(PathExpressionParser.java:283)
        at net.sf.hibernate.hql.WhereParser.doPathExpression(WhereParser.java:336)
        at net.sf.hibernate.hql.WhereParser.doToken(WhereParser.java:366)
        at net.sf.hibernate.hql.WhereParser.token(WhereParser.java:251)
        at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
        at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
        at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
        at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:152)
        at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:141)
        at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:287)
        at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1488)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1459)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1449)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1445)
        at ourcommunity.util.hibernate.HibernateHelper.find(HibernateHelper.java:303)
        at ourcommunity.article.dao.ArticleDAO.findArticlesByCategoryId(ArticleDAO.java:47)
        at ourcommunity.article.dao.ArticleDAO.main(ArticleDAO.java:79)



Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 14, 2003 8:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Nope, navigating through a multi-valued property does not work in Java either. The correct syntax is:

Code:
select a
from ArticleDO a
join a.categories  cat
where a.enabled=1 and cat.categoryId=?


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.