Import entities

Occasionally, you may have a secondary workflow or editing context that needs to be isolated from the primary workflow. Creating another EntityManager and importing entities is a great way to create a "sandbox" environment that copies and isolates changes to the entities until you are ready to merge them back into the primary workflow.

The secondary EntityManager likely needs to be seeded with entities from the primary EntityManager. If they have already been cached, this avoids having to go back to the datasource to retrieve them, but more importantly, some of the entities may be modified and you want their modified state to be copied into the secondary manager.

Import entities into an EntityManager

This creates a copy of the entities into the cache of the EntityManager. Any changes to the copied entities in this cache will not affect the source entities. Notice that the second parameter of ImportEntities takes a MergeStrategy. This MergeStrategy determines how the entities are merged if they already exist in the target EntityManager's cache.

If you are importing entities into an empty EntityManager, then you don't need to worry about merge conflicts. This only occurs when you are merging into an EntityManager which may have potentially modified versions of the same entities. For an in depth discussion about merging entities see Merge query results into the entity cache.

Work with multiple EntityManagers

Remember that when working with multiple EntityManagers that each one is a separate editing context and that you may (and probably will) have multiple copies of the same entities (entities that have the same ID or primary key).

When you are binding the entities to the UI, make sure you understand which instance of the entity you are binding to. Be very clear about which EntityManager you are using to initialize a view, and avoid mixing entities from different EntityManagers in the same view.

Find entities to import

If you don't have an easy reference to all the entities you want to import, FindEntityGraph can be of great assistance. FindEntityGraph starts with a set of root entities and retrieves all related entities for the specified associations (EntitySpans ).

FindEntityGraph does not query the datasource, so make sure that you already have the entities in cache by using the Include operator in the original query.

Temporary id remapping

During an import, any entities with a temporary id will be assigned a new temporary id to avoid conflicts with temporary ids that already exist in the cache. All foreign key references will also be remapped, so that object navigation will continue to work. Generally, you do not need to worry about this, but if you are inspecting the temp ids, this is why they change after import.