I have a Hibernate entity Parent that is joined to another: Child. In the database I have a column parent.child_id that has a foreign key constraint on child.id. Assuming I’m lazily loading, when I load an instance of Parent I expect to be able to access parent.getChild().getId() without incurring a lookup on the child table.
I expected the proxy returned by parent.getChild() to be able to fulfil a request to Child.getId() as it has already loaded the child’s id from parent.child_id column. However, I see an additional query on the child table.
Why is this extra query necessary, and how can I avoid it? In this instance I’m only interested in the foreign key ID, and do not wish to load the whole row.
Classes:
class Parent {
@Id @Column(name = "id", unique = true, nullable = false)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "child_id")
private Child child;
}
class Child {
@Id @Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "name")
private String name;
}
Tables:
CREATE TABLE child (
id int PRIMARY KEY,
name varchar(10)
);
CREATE TABLE parent (
id int PRIMARY KEY,
child_id int REFERENCES (child.id)
);
It’s caused by the fact that
Childuses field access (since annotations are placed on fields), therefore Hibernate simply initializes the proxy when you call any of its methods.If you move annotations to properies, it would work as expected.
Since JPA 2.0 (Hibernate 3.5) you can configure it in fine-grained way: