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?