Hibernate, insert or update without select

后端 未结 3 1421
温柔的废话
温柔的废话 2021-02-20 05:51

I have a products objects which belongs to certain categories i.e. classical many to one relationship.

@Entity
public class Product{

    @Id
    @GeneratedValue         


        
3条回答
  •  醉话见心
    2021-02-20 05:53

    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...)

提交回复
热议问题