-->
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.  [ 9 posts ] 
Author Message
 Post subject: querying maps using hql
PostPosted: Mon Aug 15, 2005 2:00 pm 
Newbie

Joined: Thu Aug 11, 2005 1:20 pm
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3

Mapping documents:
<class name="Child" table="childtable">

<composite-id name="ParentId" class="ParentId" >
<key-property name="DbId" column="dbid" type="long"/>
<key-property name="UserId" column="userid" type="UserIdUserType" />
</composite-id>

<map name="Attributes" lazy="false" table="ChildAttribute" cascade="all" >

<key>
<column name="DbId" not-null="true"/>
<column name="profileid" not-null="true"/>
</key>

<map-key type="string" column="attribute_name" />
<element type="string" column="attribute_value"/>
</map>

</class>
Code between sessionFactory.openSession() and session.close():

I need to query from the map of child attributes. I need to update the attribute values for all those rows where DbId in a particular set of numbers and the attribute name has a specific value.

in sql, would be something like,

update childattribute
set attribute_value = "newValue"
where attribute_name = "attrbute1"
and dbid in (1,2,3,4,5)

The query that I used is as follows.

update ChildAttribute
set Attributes[?] = ?
where ? in indices(Child.Attributes)
and id.DbId in (?, ?)


Full stack trace of any exception that occurs:


org.hibernate.hql.ast.QuerySyntaxError: expecting EQ, found '[' near line 1, column 48 [
update ChildAttribute set Attributes[?] = ?
where ? in indices(Child.Attributes) and id.DbId in (?, ?)]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)

I tried to find out what the exception meant exactly, but was unable to find anything either in the documentation or HIA. If someone could help me, it would be great. Also, if someone could give some information on using HQL with collection objects,it would be great.


Name and version of the database you are using:
oracle 9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: hql
PostPosted: Mon Aug 15, 2005 3:52 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Hibernate is O(object)-R-M(mapper) so you are supposed to get the object, update necessary attributes and store the object.

If yon need/want to update attributes the 'SQL' way then simply use Hibernates native SQL support http://www.hibernate.org/hib_docs/v3/re ... rysql.html
and risk having incorrect objects in some cache (Session or 2nd level).

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
http://www.hibernate.org/hib_docs/v3/re ... tch-direct


Top
 Profile  
 
 Post subject: querying values in maps
PostPosted: Mon Aug 15, 2005 5:53 pm 
Newbie

Joined: Thu Aug 11, 2005 1:20 pm
Posts: 8
thanks for pointing out the pt about objects. gavin - my problem is occuring with writing a query criteria for the map object.

<class name="Child" table="childtable">

<composite-id name="ParentId" class="ParentId" >
<key-property name="DbId" column="dbid" type="long"/>
<key-property name="UserId" column="userid" type="UserIdUserType" />
</composite-id>

<map name="Attributes" lazy="false" table="ChildAttribute" cascade="all" >

<key>
<column name="DbId" not-null="true"/>
<column name="profileid" not-null="true"/>
</key>

<map-key type="string" column="attribute_name" />
<element type="string" column="attribute_value"/>
</map>

</class>

how do i write the query to have the attribute_name match a specific value and attribute_value be a specific value ? I tried the following query. I need to know how to query for values inside a map ?

update ChildAttribute
set Attributes[?] = ?
where ? in indices(Child.Attributes)
and id.DbId in (?, ?)

It gives me the following exception:

org.hibernate.hql.ast.QuerySyntaxError: expecting EQ, found '[' near line 1, column 48 [
update ChildAttribute set Attributes[?] = ?
where ? in indices(Child.Attributes) and id.DbId in (?, ?)]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There is no support for updating collection of value types using HQL.


Top
 Profile  
 
 Post subject: querying values in maps
PostPosted: Mon Aug 15, 2005 6:14 pm 
Newbie

Joined: Thu Aug 11, 2005 1:20 pm
Posts: 8
ok, so the only way to do it is to retrieve each object and update it? I tried doing that, using the following.

Criteria query = session.createCriteria(ChildMember.class);
query.add(Expression.in("id.ParentId", pIds));
query.add(Expression.ilike("Attributes.attribute_name", "attr1", MatchMode.EXACT));
List result = query.list();
GL.lh("Results are " + result.toString());

It gave me the following exception. I tried Expression.ilike("Attributes[attribute_name]", "attr1") as well - but the same exception again. I have gone through the documentation and HIA, but am not able to find out how to query within collections. attribute_name and attribute_value are both strings. Would really appreciate the help.

thanks a lot,
smsea.

HibernateExceptionAdapter: caught exception: org.hibernate.QueryException: could not resolve property: userAttributes.attribute_name of: ChildMember
08/15/05 15:08:54:862: [main]: org.hibernate.QueryException: could not resolve property: userAttributes.attribute_name of: com.pogo.besl.userassociation.UserAssociationMember
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:63)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.BasicEntityPersister.toColumns(BasicEntityPersister.java:1086)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:403)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:369)
at org.hibernate.criterion.IlikeExpression.toSqlString(IlikeExpression.java:32)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)


Top
 Profile  
 
 Post subject: not null
PostPosted: Mon Aug 15, 2005 6:23 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
perhaps
query.add(Expression.isNotNull("Attributes['attr1']" );

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: querying within maps
PostPosted: Mon Aug 15, 2005 6:25 pm 
Newbie

Joined: Thu Aug 11, 2005 1:20 pm
Posts: 8
I am checking for a match with a certain value, just not null is not enough. sorry about that.


Top
 Profile  
 
 Post subject: value
PostPosted: Mon Aug 15, 2005 6:36 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
The code
Quote:
query.add(Expression.ilike("Attributes.attribute_name", "attr1", MatchMode.EXACT));

looks like it is supposed to check if we have attribute with name (case independent) 'attr1' - that is that my isNotNull statement will do but in case dependent matter unless you keep null values in the map.

If you are interested in knowing if an element of the collection with a particular name has a particular value then it probably looks like:

query.add(Expression.ilike("Attributes['attrName']", "attrValue", MatchMode.EXACT));

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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