jbarz
jbarz

Reputation: 592

When does retrieving a Hibernate proxy's id initialize the proxy?

In Hibernate when you retrieve an Entity via a load, if you access that Entity's id accessor it does not initialize the proxy:

Property myProp = (Property) session.load(Property.class, myId);
myProp.getId(); // Doesn't load the proxy
myProp.getDescription();  // Loads the proxy from the database hit

However, I am unclear what the rules are for loading association proxies. I was under the impression that when you retrieve an Entity via a HQL query or Criteria the same rules apply.

Each Property in my code has a Company. The Company is declared as such:

@ManyToOne(fetch = FetchType.LAZY, optional=false)
@JoinColumn(name = "company_id", nullable=false)
private Company company;

However, in my code I'm currently getting N+1 selects when iterating through the Properties because the Company is being loaded when I call Company.getId().

In both the Company and Property object the id field is a Long annotated with @Id.

Is there some other consideration I'm missing in order to prevent the loading of the Company entity?

Upvotes: 14

Views: 5046

Answers (1)

Arthur Ronald
Arthur Ronald

Reputation: 33785

It does not work as expected simply because of you have to use property access instead of field access.

Instead of

@ManyToOne(fetch=FetchType.LAZY, optional=false)
@JoinColumn(name="COMPANY_ID", nullable=false)
private Company company;

Use

@ManyToOne(fetch=FetchType.LAZY, optional=false)
@JoinColumn(name="COMPANY_ID", nullable=false)
public Company getCompany() {
    return this.company;
}

Takes care you can not use property access and field acces at the same time. So you should choose one approach.

regards,

Upvotes: 11

Related Questions