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

Use property access strategy

Instead of

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

Use

private Bar bar;

@OneToOne(fetch=FetchType.LAZY, optional=false)
public Bar getBar() {
    return this.bar;
}

Now it works fine!

A proxy is initialized if you call any method that is not the identifier getter method. But it just works when using property access strategy. Keep it in mind.

See: Hibernate 5.2 user guide

Leave a Comment