-->
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: SELECT and GROUP BY an associated entity
PostPosted: Tue Dec 16, 2008 11:18 am 
Newbie

Joined: Thu Sep 11, 2008 10:19 am
Posts: 6
Hi folks,

I've a problem with a group by clause in a HQL statement. My application is an EAR project using Seam, Hibernate and JPA on a JBoss AS. The entities are defined by Hibernate Annotations.
I'm not at work so I try to build an example that shows my problem. Let's assume I've an entity "Document" and "User". A document is created by an user called "editor". Every document is of a specific type. This would lead to something like this:

Code:
@Entity
@Table("document")
public class Document {
  @Column("editor")
  @ManyToOne(//don't know ATM)
  private User editor;

  @Column("type")
  @ManyToOne
  private Type type
//getters, setters and so on
}

@Entity
@Table("user")
public class User {
  @Column("name")
  private String name
//getters, setters and so on
}

@Entity
@Table("type")
public class Type {
  @Column("name")
  private String name
//getters, setters and so on
}



Now I want to create an overview of editors and the count of their documents of a specific type. In SQL this would be something like this:

Code:
select u.name, count(*) from user u, document d where u.id = d.editor and d.type = xyz group by u.name


I tried this in HQL:

Code:
select editor, count(*) from document group by user


The problem is that Hibernate generates a SQL statement like this:

Code:
select u.id, u.name, u.xxx from user u, document d where u.id = d.editor group by u.id


Of course this is an invalid combination of select and group by. Has anyone an idea how I could collect the data I need in a fast way (the solution I had was too slow)?

Thanks in advance
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 12:49 pm 
Newbie

Joined: Thu May 01, 2008 9:34 pm
Posts: 7
try this.

select e.name, count(*) from document d join d.editor e where d.type.id = :id group by e.name


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 4:27 am 
Newbie

Joined: Thu Sep 11, 2008 10:19 am
Posts: 6
Thank you. It works. But since I want to have the editor as an entity (the classes shown don't contain every member ;)), I had to extend the statement like this:

Code:
select e, count(*) from document d join d.editor e where d.type.id = :id group by e.name, e.member2, e.member3


It's not very elegant, but it works.


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.