I have a products objects which belongs to certain categories i.e. classical many to one relationship.
@Entity
public class Product{
@Id
@GeneratedValue
session.load() exists specifically for cases like this. The following:
Category category = session.load(categoryId);
product.setCategory(category);
will not hit the database. It will, however, throw an exception at a later stage (during flush, more or less) if there is no category available with given id.
Using load()
is faster than merge()
and has no side-effects (cascading, etc...)