The load method returns a proxy and hits the database only when you access a filed of the entity. If any entity is found the load method throws an ObjectNotFounfException but only when you try to access the entity fields.
The get method hits the database as soon as it's called and if no entity is found returns null.
When to use get and when load?
The load method increases perfromance (hits the database only when necessary) so if you know your entity will be used only within the unit of work where it's retrieved than the load method is probably the best choice but if the retrieved entity is used outsite the transactional unit of work, e.g. it's displayed on a web page, using the get method is probably better.
Nessun commento:
Posta un commento