Does EntityManager's find() method create new instance of JPA class?

后端 未结 2 423
情书的邮戳
情书的邮戳 2021-01-02 19:27

I\'m a bit confused. The question is in title, and here\'s why I\'m asking. I have JSF + JPA web-application running on a single VM. And an JPA class has @Transient<

2条回答
  •  迷失自我
    2021-01-02 19:29

    If you invoke find(..) within the same session (that is, within the same entitymanager lifetime), then the same object reference will be returned. The documentation of find() specifies this:

    If the entity instance is contained in the persistence context, it is returned from there.

    In other words, the EntityManager holds a collection (map most likely) of entities. When you call find it checks that collection. If the entity is not found there, a query to the database is made. The returned entity is put into the map, so subsequent calls will find it there.

    But note again that this is only for the span of one session. This is usually the same as one http request (in the web app context)

提交回复
热议问题