Hibernate one-to-one: getId() without fetching entire object

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-04-07T15:27:13Z Indexed on 2010/04/07 17:43 UTC
Read the original article Hit count: 183

Filed under:
|

I want to fetch the id of a one-to-one relationship without loading the entire object. I thought I could do this using lazy loading as follows:

class Foo { 
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    private Bar bar; 
}


Foo f = session.get(Foo.class, fooId);  // Hibernate fetches Foo 

f.getBar();  // Hibernate fetches full Bar object

f.getBar().getId();  // No further fetch, returns id

I want f.getBar() to not trigger another fetch. I want hibernate to give me a proxy object that allows me to call .getId() without actually fetching the Bar object.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about jpa