-->
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.  [ 5 posts ] 
Author Message
 Post subject: one-to-one id without fetching
PostPosted: Mon Feb 02, 2009 9:19 am 
Newbie

Joined: Mon Feb 02, 2009 8:56 am
Posts: 6
Hi:

I've got two related classes as shown here:

Employee ------> Photograph

Every employee has got a photo. It's a one-to-one association set to "Fetch.LAZY", because I don't want the photograph to be retrieved every time I get information about an employee.

Sometimes I need an employee's photo id (or null, if none), but I still don't want to retrieve the whole image.
So I do "myEmployee.getPhotograph().getId()" but I can see Hibernate is querying the database and selecting Photograph table to load the photo and give me the id. But, as the id is already in the Employee table (used by joinColumn), isn't it possible to avoid hitting the database for the photo? I just want the id!

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 9:21 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Could you provide your mapping and the code where you access it?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 9:28 am 
Newbie

Joined: Mon Feb 02, 2009 8:56 am
Posts: 6
mmerder wrote:
Could you provide your mapping and the code where you access it?


This is my Employee class:

Code:
@Entity
public class Employee {
...

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idPhotograph")
private Photograph photo;
...


My Photograph class:

Code:
@Entity
public class Photograph {
...
}


Code where I'm accessing it:

Code:
...
Employee emp = EmpService.find(id);
System.out.println("Photo id: " + emp.getPhotograph().getId());


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 9:31 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I also faced this problem. You need to annotate your getters instead of your fields. Only when you annotate your getters, hibernate knows, which one is the id-getter and so does not initialize a proxy. Notice that it's not enough just to annotate one specific getter, but annotate all getters instead of fields within a class.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 11:05 am 
Newbie

Joined: Mon Feb 02, 2009 8:56 am
Posts: 6
mmerder wrote:
I also faced this problem. You need to annotate your getters instead of your fields. Only when you annotate your getters, hibernate knows, which one is the id-getter and so does not initialize a proxy. Notice that it's not enough just to annotate one specific getter, but annotate all getters instead of fields within a class.

Yes, you're right, it works ;-)

Thank you very much


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