Difference between find () and getreference () of entitymanager

Source: Internet
Author: User

Similarities

Both methods take the class of the object and the object representing the primary key of the object as the parameter. Because they use Java generic methods, they can obtain object objects of specific types without any explicit type conversion. Specifically, autoboxing (automatic packing) of Java 5 is widely used in primarykey.

In addition, both of them are thrown when entitymanager is disabled.IllegalStateException-If this entitymanager has been closed. If the first parameter is not an object or the second parameter is not a valid primary key

IlegalArgumentException-If the first argument does not denote an entity type or the second argument is not a valid type for that entity's primary key

 

 

Differences:

Find () returns the object of the specified oid. If the object exists in the current persistence context, the returned value is the cached object; otherwise, a new object is created, load the related persistent state from the database. If the database does not have the specified OID record, the find () method returns NULL.

The getreference () method is similar to find. The difference is that if no specified entity is in the cache, entitymanager creates a new entity, but does not immediately access the database to load the persistent state, it is loaded only when a property is accessed for the first time. In addition, the getreference () method does not return NULL. If the database cannot find the corresponding entity, this method will throw javax. Persistence. entitynotfoundexception.

  • EntityNotFoundException-If the entity state cannot be accessed

In some cases, the getreference () method can be used to avoid the performance overhead of loading the persistent state from the database.

 

Here we will focus on two sentences:

If no specified object exists in the cache, entitymanager creates a new object, but does not immediately access the database to load the persistent state. Instead, it is loaded only when a certain attribute is accessed for the first time.

For example, Em. we can perform various operations on the object returned by find. because the entity returned by getreference () does not access the database immediately to load the persistent state, operations on it are likely to produce exceptions. For example, when you perform getter operations on the entity it returns, because entitymanager uses delayed loading, org. hibernate. lazyinitializationexception cocould not initialize proxy no session

Therefore, the find () method is usually used when a new entity is passed to the transaction. When the database is not connected, the getter method is not used, the getreference () method is used only when the setter method is used to change the status. (This is because getreference returns a proxy entity, that is, the persistent state is not loaded)

 

In some cases, the getreference () method can be used to avoid the performance overhead of loading the persistent state from the database.

This is also because getreference returns a proxy entity.

For example, in a simple update operation, use find () to obtain the object, and then use the setter method of the object; or the getreference () method, and then use the setter method of the object.

For the SQL statement called by the former JPA: Select ***, and then update ****

For the latter: Only update *****

 

Another example:

SQL statement executed by the Operation

Em. Remove (Em. getreference (person. Class, 1) delete from person where personid = 1

Em. Remove (Em. Find (person. Class, 1) Select * From person where personid = 1

Delete from person where personid = 1

 

It can be seen that find () performs a select operation, while getreference does not perform database operations, but returns a proxy, this reduces the overhead of connecting to the database and loading the persistent state from the database.

Another link:

Http://blog.csdn.net/hongtaq/article/details/8026798


Difference between find () and getreference () of entitymanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.