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>
|