3.4.3.1. PersistenceTools
A Spring bean containing helper methods related to data storage functionality. It can be obtained either by calling the Persistence.getTools()
method or like any other bean, through injection or the AppBeans
class.
The PersistenceTools
bean has the following methods:
-
getDirtyFields()
– returns a collection of entity attribute names that have been changed since the last load of the instance from the DB. For new instances an empty collection is returned. -
isLoaded()
– determines if the specified instance attribute was loaded from the DB. The attribute may not be loaded, if it was not present in the view specified when loading the instance.This method only works for instances in the Managed state.
-
getReferenceId()
– returns an ID of the related entity without loading it from the DB.Let us suppose that an
Order
instance was loaded in the persistent context and it is necessary to get the ID value of theCustomer
instance related to thisOrder
. A call to theorder.getCustomer().getId()
method will execute the DB query to load theCustomer
instance, which in this case is unnecessary, because the value of the Customer ID is also located in theOrder
table as a foreign key. Whereas the execution ofpersistence.getTools().getReferenceId(order, "customer")
will not send any additional queries to the database.
This method works only for instances in the Managed state.
The PersistenceTools
bean can be overridden in your application to extend the set of default helper methods. An example of working with the extended interface is shown below:
MyPersistenceTools tools = persistence.getTools();
tools.foo();
((MyPersistenceTools) persistence.getTools()).foo();