-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL Question about querying via relation
PostPosted: Thu Jul 21, 2005 2:25 am 
Newbie

Joined: Wed May 18, 2005 3:02 pm
Posts: 18
I have a user object with a one-to-one mapping to another object. I know the id of that other object, and I want to load the appropriate user. It is a unidirectional relation from the user to the other object, so I can't load the child object by id and then do a getUser() on it, not that I'm interested in executing 2 queries just get the user object when I've got a perfectly acceptable unique identifier.

The docs don't make clear whether a Criteria object (I've been using Criteria up until now) takes a column name or a property name, so I don't know if I can add an Expression like Expression.eq("TOKEN_ID", tokenId) - which is the column name - or like Expression.eq("token.id", tokenId) - which is the nested property accessor.

Alternatively, I could use raw HQL, but the 4 examples in the docs don't really make clear how I would load the parent object via the child id. I'm going to attempt a couple of different strategies, but figured I'd post my question here so that maybe I can get an answer before I tear my hair out trying various things

Here's a (simplified) class and mapping description

User - properties
Long id
String login
String password
UserToken token

UserToken - properties
String id

I have the token.id String, and I want to load a user object.

<class name="User" table="user">
<id name="id" type="long" unsaved-value="null" >
<column name="USER_ID" not-null="true"/>
<generator class="hilo"/>
</id>

<property name="login">
<column name="LOGIN" length="32" not-null="true" unique="true"/>
</property>
<property name="password">
<column name="PASSWORD" length="32" not-null="true"/>
</property>

<many-to-one name="token"
column="TOKEN_ID"
unique="true"
cascade="all,delete-orphan" />
</class>


<class name="UserToken" table="usertoken">
<id name="id" type="string" unsaved-value="null" >
<column name="USERTOKEN_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 2:30 am 
Newbie

Joined: Wed May 18, 2005 3:02 pm
Posts: 18
Forget it. My very first attempt worked. I thought I'd save time by posting before trying to find the answer by trial and error, and it wound up costing me the time I took to write it up.

For anyone searching for a similar answer, here's what i did:

User user = (User) session.createCriteria(User.class)
.add( Expression.eq("token.id", tokenId))
.uniqueResult();


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