I have a products objects which belongs to certain categories i.e. classical many to one relationship.
@Entity
public class Product{
@Id
@GeneratedValue
Hibernate needs a safe way to determine the state of your object. That's why it's going a long way to make sure you can't mix objects from different sessions (so you can't load the categories at startup and then just use them later).
The solution is to load the categories at startup, set up a caching provider like ehcache
for this class and then use this code:
Product product = new Product(somename);
product.setCategory(session.merge(category));
That won't cause any DB round trips if you configure the cache that category instances can't change.