posted 10 years ago
will return a fully loaded object of the User entity with userId = 1 if found. it returns null otherwise
will return a proxy User object with the identifier property populated without hitting the database. It hits the DB only when you attempt to access a non-identifier property and it is then that it throws an exception if a record with userId = 1 is not found. Does not throw an exception while you are accessing the identifier property only.
Now, given a scenario where in a e-commerce system User is creating a Bid. Bid has a one-to-one association with User. When a Bid object is created, its user attribute needs to be populated with a User object. When the Bid object is subsequently saved in the Bid table the foreign key reference to User table needs to be set. Given this, you may want to ask yourself ; do you really want a fully loaded User object in Bid just for setting the value of a foreign key in Bid table OR is it more than sufficient to achieve the same thing with a proxy.
Think, Learn, Teach, Invent, Innovate, Build, Deliver