What is the difference between EntityManager.find() and EntityManger.getReference()?

后端 未结 3 1362
余生分开走
余生分开走 2020-12-02 06:35

Whats is the difference between

 T EntityManager.find(Class entityClass, Object primaryKey) and 
 T EntityManager.getReference(Cl         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 07:25

    getReference() does not retrieve the full object but only a proxy and therefore can be more efficient if you do not access the members of the object.

    For instance when creating a new object to insert into your database, it might have to refer to another object which already has been stored in the database.

    For JPA to store the new object correctly only the primary key of the referred object is needed. By using getReference() you get a proxy which contains the primary key and you save the cost of loading the complete object.

提交回复
热议问题