# Object-Relational Mapping: Background

OUAF uses an Object-Relational Mapping (ORM) engine, which maps tables to entities using the system's table, table/field, field, and constraint metadata to guide the creation of mapping definitions during artifact generation.

Entities represent database tables. They are created as Java objects during a database "session", which has the lifetime of a single DB transaction.

DONT: Once the session that created them has ended, entities are not safe to use for reference, calling methods, and so on. For example, don't copy entities into application caches.

DO: Instead, let the application cache do the data retrieval and return the data to the session. `Id` objects are safe to store across sessions. Note in the following example that the entity `AlgorithmType` is not stored:

    			public class AlgorithmTypeInfoCache implements ApplicationCache {
    			  private static final AlgorithmTypeInfoCache INSTANCE = new AlgorithmTypeInfoCache();
    			  private final ConcurrentMap<AlgorithmType_Id, AlgorithmTypeInfo> algorithmTypeInfoById = new ConcurrentHashMap<AlgorithmType_Id, AlgorithmTypeInfo>();
    			  protected AlgorithmTypeInfoCache() {  ContextHolder.getContext().registerCache(this);    }
    			  public String getName() {  return "AlgorithmTypeInfoCache";    }
    			  public void flush() {algorithmTypeInfoById.clear();    }
    			  public static AlgorithmTypeInfo getAlgorithmTypeInfo(AlgorithmType_Id algTypeId) {
    			    return INSTANCE.privateGetAlgorithmTypeInfo(algTypeId);
    			  }

    			  private AlgorithmTypeInfo privateGetAlgorithmTypeInfo(AlgorithmType_Id algTypeId) {
    			    AlgorithmTypeInfo algTypeInfo = algorithmTypeInfoById.get(algTypeId);
    			    if (algTypeInfo != null) return algTypeInfo;
    			    AlgorithmType type = algTypeId.getEntity();
    			    if (type == null) return null;
    			    AlgorithmTypeInfo info = new AlgorithmTypeInfo(type);
    			    AlgorithmTypeInfo prev = algorithmTypeInfoById.putIfAbsent(algTypeId, info);
    			    if (prev != null) return prev;
    			    return info;
    			  }
    			}

DO: it is safe to use XML documents (to be consumed by BOs, BSs, or SSs) for moving data between sessions.

Every entity has a unique corresponding `Id` class, for example: `BatchControl` has `BachControlId`. The ORM framework automatically generates correct SQL to perform the following essential tasks:

- Read, update, insert, delete one entity (row) from the database.
- Navigate between related entities as per their key/constraint relationships. For example, from a parent entity to a collection of children.

- The ORM defers database calls for performance
- ID Objects
- Counting a collection
- Avoid unnecessary work
- ORM 'Navigation' is your friend
- How to Pre-load Entities Using Fetch
- Session Cache
- Level 2 Cache Applicable for Batch
- Flushing - COBOL and Save Points

Parent topic: Development Performance Guidelines