-->
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: How to do query against Hibernate Object with composite id?
PostPosted: Wed Sep 29, 2004 2:42 pm 
Newbie

Joined: Tue Sep 14, 2004 1:35 pm
Posts: 9
Hibernate version:
2.1
Mapping documents:
I have a USERLOG table and an ACTIVITY table. the relationship is one-to-many respectively.

USERLOG has a primary key made up of userid and date fields.
ACITITY has a foreign key made up of userid and date fields
and a primary key made up of userid, date and taskno fields

So, any user can have many entries in ACTIVITY table on a given date.

How do I do query like this in Hibernate
I want to query total count of taskno and group by taskno

SELECT taskno, count(taskno) from ACTIVITY group by taskno order by taskno

Mapping files

<hibernate-mapping>
<class
name="com.test.model.Userlog" >
table="evalogs"
<composite-id name="comp_id" class="com.test.model.UserlogPK" unsaved-value="any">
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="userid"
column="userid"
type="java.lang.Integer"
length="4"
>
</key-property>
<key-property
name="date"
column="date"
type="java.lang.String"
length="10"
>
</key-property>
</composite-id>


<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->

<!-- bi-directional one-to-many association to Activity -->
<set
name="activity"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="userid" />
<column name="date" />
</key>
<one-to-many
class="com.test.model.Activity"
/>
</set>

</class>
</hibernate-mapping>


<hibernate-mapping>
<class
name="com.test.model.Activity"
table="activity"
>

<composite-id name="comp_id" class="com.test.model.ActivityPK" unsaved-value="any">
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="userid"
column="userid"
type="java.lang.Integer"
length="4"
>
</key-property>
<key-property
name="date"
column="date"
type="java.lang.String"
length="10"
>
</key-property>
<key-property
name="taskNo"
column="taskno"
type="java.lang.Integer"
length="4"
>
</key-property>
</composite-id>


<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Userlog -->
<many-to-one
name="Userlog"
class="com.test.model.Userlog"
update="false"
insert="false"
>
<meta attribute="field-description">
@hibernate.many-to-one
update="false"
insert="false"
</meta>
<meta attribute="field-description">
@hibernate.column
name="date"
</meta>
<meta attribute="field-description">
@hibernate.column
name="userid"
</meta>
<column name="userid" />
<column name="date" />
</many-to-one>

<!-- end of derived association(s) -->


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

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Postgresql 7.4.1
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 3:00 pm 
Newbie

Joined: Tue Sep 14, 2004 1:35 pm
Posts: 9
Continuation...

Debug trace..

[9/29/04 11:45:41:699 EDT] 15f7df77 EvaluationDAO I com.test.dao.hibernate.EvaluationDAOHibernate get all activities:
[9/29/04 11:45:41:761 EDT] 15f7df77 DispatcherSer E org.springframework.web.servlet.DispatcherServlet Could not complete request
[9/29/04 11:45:41:777 EDT] 15f7df77 DispatcherSer E org.springframework.web.servlet.DispatcherServlet TRAS0014I: The following exception was logged org.springframework.orm.hibernate.HibernateQueryException: undefined alias: taskNo [select taskNo, count(taskNo), sum(grades) from com.test.model.Activity group by taskNo order by taskNo]; nested exception is net.sf.hibernate.QueryException: undefined alias: taskNo [select taskNo, count(taskNo), sum(grades) from com.test.model.Activity group by taskNo order by taskNo]
net.sf.hibernate.QueryException: undefined alias: taskNo [select taskNo, count(taskNo) from com.test.model.Activity group by taskNo order by taskNo]
at net.sf.hibernate.hql.PathExpressionParser.token(PathExpressionParser.java:103)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.SelectParser.token(SelectParser.java:170)
at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
at net.sf.hibernate.hql.ClauseParser.end(ClauseParser.java:114)
at net.sf.hibernate.hql.PreprocessingParser.end(PreprocessingParser.java:143)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:30)
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 org.springframework.orm.hibernate.HibernateTemplate$22.doInHibernate(HibernateTemplate.java:416)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:176)
at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:196)
at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:413)
at com.test.dao.hibernate.ActivityDAOHibernate.getAllEvaluatedGroupByQnum(ActivityDAOHibernate.java:63)
at com.test.service.impl.ActivityManagerImpl.getAllEvaluatedGroupByQnum(ActivityManagerImpl.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:116)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:152)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 8:05 pm 
Newbie

Joined: Tue Sep 14, 2004 1:35 pm
Posts: 9
Sorry, I'm not getting a response from Spring forum. They are saying this is more of a Hibernate question. Can someone help?

If I apply this query in the getHibernateTemplate() such as

public List getAllActivitiesGroupByTaskNo() {
log.info("get all activities: ");
return getHibernateTemplate().find("select taskNo, count(taskNo) from Activity group by taskNo order by taskNo");
}

I would get undefined alias taskNo
which is true because taskNo is not a property of Activity class. taskNo is defined in ActivityPK class which is referenced by Activity class as described in the hbm.xml file

My question is how would I query against a Hibernate Object with composite id with the above sql statement?

Here are the java objects for the tables:

public class Userlog implements Serializable {
/** identifier field */
private com.test.model.UserlogPK comp_id;
/** persistent field */
private Set activites;
/** full constructor */
public Userlog(com.test.model.UserlogPK comp_id, Set activities) {
this.comp_id = comp_id;
this.activities = activities;
}
/** default constructor */
public Userlog() {
}
/**
* @hibernate.id
* generator-class="assigned"
*
*/
public com.test.model.UserlogPK getComp_id() {
return this.comp_id;
}
public void setComp_id(com.test.model.UserlogPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.set
* lazy="true"
* inverse="true"
* cascade="none"
* @hibernate.collection-key
* column="userid"
* @hibernate.collection-key
* column="date"
* @hibernate.collection-one-to-many
* class="com.test.model.Activity"
*
*/
public Set getActivities() {
return this.activities;
}
public void setActivities(Set activities) {
this.activities = activities;
}
public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Evalog) ) return false;
Evalog castOther = (Evalog) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}

public class UserlogPK implements Serializable {
/** identifier field */
private Integer userid;
/** identifier field */
private String date;
/** full constructor */
public UserlogPK(Integer userid, String date) {
this.userid = userid;
this.date = date;
}
/** default constructor */
public UserlogPK() {
}
/**
* @hibernate.property
* column="userid"
* length="4"
*
*/
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getDate() {
return this.date;
}
public void setDate(String date) {
this.date = date;
}
public String toString() {
return new ToStringBuilder(this)
.append("userid", getUserid())
.append("date", getDate())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof UserlogPK) ) return false;
UserlogPK castOther = (UserlogPK) other;
return new EqualsBuilder()
.append(this.getEvaluated(), castOther.getEvaluated())
.append(this.getEvaluator(), castOther.getEvaluator())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getEvaluated())
.append(getEvaluator())
.toHashCode();
}
}

Activity class:
public class Activity implements Serializable {
/** identifier field */
private com.test.model.ActivityPK comp_id;
/** nullable persistent field */
private com.test.model.Userlog userlog;
/** full constructor */
public Activity(com.test.model.ActivityPK comp_id, com.test.model.Userlog userlog) {
this.comp_id = comp_id;
this.userlog = userlog;
}
/** default constructor */
public Activity() {
}
/** minimal constructor */
public Activity(com.test.model.ActivityPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.id
* generator-class="assigned"
*
*/
public com.test.model.ActivityPK getComp_id() {
return this.comp_id;
}
public void setComp_id(com.test.model.ActivityPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.many-to-one
* update="false"
* insert="false"
*
* @hibernate.column
* name="userid"
*
* @hibernate.column
* name="date"
*
*/
public com.test.model.Userlog getEvalog() {
return this.userlog;
}
public void setEvalog(com.test.model.Userlog userlog) {
this.userlog = userlog;
}
public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Activity) ) return false;
Activity castOther = (Activity) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}

public class ActivityPK implements Serializable {
/** identifier field */
private Integer userid;
/** identifier field */
private String date;
/** identifier field */
private Integer taskNo;
/** full constructor */
public ActivityPK(Integer userid, Integer date, Integer taskNo) {
this.userid = userid;
this.date = date;
this.taskNo = taskNo;
}
/** default constructor */
public ActivityPK() {
}
/**
* @hibernate.property
* column="date"
* length="4"
*
*/
public Integer getDate() {
return this.date;
}
public void setDate(Integer date) {
this.date = date;
}
/**
* @hibernate.property
* column="userid"
* length="4"
*
*/
public Integer getEvaluated() {
return this.userid;
}
public void setEvaluated(Integer userid) {
this.userid = userid;
}
public Integer getTaskNo() {
return this.taskNo;
}
public void setTaskNo(Integer taskNo) {
this.taskNo = taskNo;
}
public String toString() {
return new ToStringBuilder(this)
.append("userid", getUserid())
.append("date", getDate())
.append("taskNo", getTaskNo())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof ActivityPK) ) return false;
ActivityPK castOther = (ActivityPK) other;
return new EqualsBuilder()
.append(this.getUserid(), castOther.getUserid())
.append(this.getDate(), castOther.getDate())
.append(this.getTaskNo(), castOther.getTaskNo())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getUserid())
.append(getDate())
.append(getTaskNo())
.toHashCode();
}
}


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.