Sam
Sam

Reputation: 8693

How do I just fetch the primary key(s) alone from a table

I have an entity public class GroupUser { Group g; User u; }. How do I fetch just the primary key(s) of all users. (i.e. List of u.getId() for a query where you fetch all users which belong to a group). I don't want to fetch the entire row, since it seems to take more time right now

Upvotes: 1

Views: 230

Answers (3)

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

In an HQL query, you can use the property id to refer to an entity's primary key (assuming that it doesn't define a non-pkey property called "id", of course). Thus you could constrain an HQL query with a select clause as so:

select u.id
from Groups g inner join Users u

where the inner join excludes any users that don't belong to a group. Of course you could apply any standard constraints to this query as well.

Upvotes: 2

hvgotcodes
hvgotcodes

Reputation: 120168

You would want to use hql.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html

Upvotes: 1

Jorge
Jorge

Reputation: 18237

if you need the query it's pretty simple

select u.Id from User u and put it in a Collection of long

Upvotes: 2

Related Questions