-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query is assuming a method as a property
PostPosted: Tue Mar 22, 2005 2:09 pm 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:31 am
Posts: 20
Hibernate version: 2.17


I have 2 classes: Task and Group (TASK and GROUP tables respectively). There is one-to-many association from Task to Group.

public class Task implements Serializable {
private taskId Integer;
...

public Integer getTaskId() {
return taskId;
}
private void setTaskId(Integer taskId) {
this.taskId = taskId;
}
...
}


public class Group implements Serializable {
private groupId Integer;
private Task task;
...

public Integer getGroupId() {
return groupId;
}
private void setGroupId(Integer groupId) {
this.groupId = groupId;
}
public Task getTask() {
return task;
}
public void setTask(Task task) {
this.task = task;
}
...
}

Everything is working ok (I mean all CRUD operations and stuff).

For a particular taskId I want to know how many Group(s) exist. The SQL would be :
SELECT count(*) FROM GROUP WHERE task_id = ?

Now I want it to do through Hibernate Query Language. So I wrote this code:

Integer taskId = ... // task Id to check
String hQuery = "select count(*) from Group g where g.task.getTaskId() = :taskid";
Query query = HibernateUtil.getSession().createQuery( hQuery );
query.setInteger( "taskid", taskId.intValue() );
Integer rows = (Integer)query.uniqueResult();

I got an error message :

2005-03-22 12:14:35,246 ERROR [STDERR] net.sf.hibernate.QueryException: could not resolve property: getTaskId of: com.trax.scheduler.model.Task [select count(*) from com.trax.scheduler.model.Group g where g.task.getTaskId() = :taskid]
at net.sf.hibernate.persister.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
at net.sf.hibernate.hql.PathExpressionParser.getPropertyType(PathExpressionParser.java:249)
at net.sf.hibernate.hql.PathExpressionParser.end(PathExpressionParser.java:288)
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:149)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:293)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1561)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1532)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:541)
...


Hibernate is wrongly assuming the getTaskId() method of Task class as a property.

Any suggestion?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 3:06 pm 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:31 am
Posts: 20
if i change the query to :

String hQuery = "select count(*) from Group g where g.task.taskId = :taskid";

it works. And Hibernate is smart enough to not load the task object first before executing the above query.
Looks like the methods are not supported in Hibernate query???


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 6:13 pm 
Newbie

Joined: Mon Jun 13, 2005 6:04 pm
Posts: 1
I had a similar issue and found the same thing that you did. The classes that I am using have composite keys, so I have methods within the classes that return a specific property from within the key. However, that won't work in HQL - I think we need to use direct properties only. So, for example if I have
classes QO, which has a primary key with properties j and q, and I want to refer to property q of the PK, even if I have a getQ() method, I have to say

Code:
where qo.comp_id.q = :value


Thanks for posting what you did. I'm trying to pull a group of records out of a set of 100K records, and the only way I can do it that I can see is by using HQL to grab just what I need from the database. I was stuck on the syntax for using the comp_id properties until I saw this thread.

Dave


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