-->
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: Querying according to one of the keys in composite-id
PostPosted: Mon Dec 06, 2004 6:23 am 
Newbie

Joined: Mon Dec 06, 2004 5:51 am
Posts: 8
Location: Istanbul/Turkiye
Hibernate version: 2.1.6
Name and version of the database you are using: Oracle 9i

I have an object with a composite-id and I have the composite-id as a seperate class. I can load the instance by giving whole composite-id attributes but I want to retrieve the row with a maximum of one of the key columns.

Example: (from Hibernate references)

User class has a field UserID class which has two fields (userName, organizationUnit) and these are composite-id in the User.xml like this.
<class name="User" table="USER">
<composite-id name="userId" class="UserId">
<key-property name="userName" column="USERNAME"/>
<key-property name="organizationId" column="ORGANIZATION_ID"/>
</composite-id>
<property ...
...
</class>

The UserID class looks like :

public class UserId extends Serializable
{
private String username;
private String organizationId;

public UserId(String username, String organizationId)
{
this.username = username;
this.organizationId = organizationId;
}

// Getters...

public boolean equals(Object o) {....}
public int hashCode() {....}
}

I can load an instance with the code:

UserId id = new UserId("john", 42);
User user = (User) session.load(User.class, id);

but I want to query something like this: (here is the question)

max(organizationId) from User where userName like "john"

I know sample is nonsense but my problem is something like that.

Note: I searched the FAQ, I hope I didn't miss a post like that...

_________________
thanks


Top
 Profile  
 
 Post subject: calling object.objectID works
PostPosted: Mon Dec 06, 2004 12:55 pm 
Newbie

Joined: Mon Dec 06, 2004 5:51 am
Posts: 8
Location: Istanbul/Turkiye
I hate finding the answer after posting a message but after trying all the possibilities I found the solution.

Query q = hSession.createQuery("select max(u.userId.organizationId) from User u where u.userId.userName = :pName")

this worked for me....

_________________
thanks


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.