-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting entity id without query
PostPosted: Wed Apr 23, 2008 8:10 am 
Newbie

Joined: Wed Apr 23, 2008 8:03 am
Posts: 2
Hi,

I guess it might be a simple question, but i cant get a solution (if there is any).

Lets say i have this mapping:

class Document{
Integer id;
MyFileType file;
.....
}

class MyFileType{
Integer id;
Byte[] file;
}

Now somewhere in code i do a
Document document = session.load(Document.class, someId);

Is there any chance i can get document.file.id without actually doing another query or loading the whole file entity? Document.getFile().getId() loads the whole entity.

Anyway, the file.id is somewhere in the document object (the proxy)...


Thx in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 2:29 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Yes. Do an HQL query returning only that field, document.file.id, and retrieve it as a string with query.uniqueResult.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 4:21 pm 
Newbie

Joined: Wed Apr 23, 2008 8:03 am
Posts: 2
I got it.

You can do:

session.getIdentifier(document.getFile());

and you get the identifier without an extra query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 6:00 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
no, I mean an HQL query

Code:
  Query query=session.createQuery("select f.id from document.file f where document.id=xx");

  String fileId=(String)query.uniqueResult();

_________________
Gonzalo Díaz


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