Cayenne (cayenne-doc: Cayenne Documentation 5.0-M2 API)
Description: declaration: package: org.apache.cayenne, class: Cayenne

# Class Cayenne

java.lang.Object
org.apache.cayenne.Cayenne

public class Cayenne extends Object
Various utils for processing persistent objects and their properties

PersistentObjects and Primary Keys: All methods that allow to extract primary key values or use primary keys to find objects are provided for convenience. Still the author's belief is that integer sequential primary keys are meaningless in the object model and are pure database artifacts. Therefore relying heavily on direct access to PK provided via this class (or other such Cayenne API) is not a clean design practice in many cases, and sometimes may actually lead to security issues.

Since:
  3.1 its predecessor was called DataObjectUtils

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Cayenne()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Map<String,Object>`
  `compoundPKForObject(Persistent object)`
  Returns a primary key map for a persistent object.
  `static ClassDescriptor`
  `getClassDescriptor(Persistent object)`
  Returns class descriptor for the object or null if the object is not registered with an ObjectContext or descriptor was not found.
  `static ObjEntity`
  `getObjEntity(Persistent p)`
  Returns mapped ObjEntity for object.
  `static PropertyDescriptor`
  `getProperty(Persistent object, String propertyName)`
  Returns property descriptor for specified property.
  `static int`
  `intPKForObject(Persistent object)`
  Returns an int primary key value for a persistent object.
  `static long`
  `longPKForObject(Persistent object)`
  Returns an int primary key value for a persistent object.
  `static String`
  `makePath(String... pathParts)`
  Constructs a dotted path from a list of strings.
  `static <T> T`
  `objectForPK(ObjectContext context, Class<T> persistentObjectClass, int pk)`
  Returns an object matching an int primary key.
  `static <T> T`
  `objectForPK(ObjectContext context, Class<T> persistentObjectClass, Object pk)`
  Returns an object matching an Object primary key.
  `static <T> T`
  `objectForPK(ObjectContext context, Class<T> persistantObjectClass, Map<String,?> pk)`
  Returns an object matching a primary key.
  `static Object`
  `objectForPK(ObjectContext context, String objEntityName, int pk)`
  Returns an object matching an int primary key.
  `static Object`
  `objectForPK(ObjectContext context, String objEntityName, Object pk)`
  Returns an object matching an Object primary key.
  `static Object`
  `objectForPK(ObjectContext context, String objEntityName, Map<String,?> pk)`
  Returns an object matching a primary key.
  `static Object`
  `objectForPK(ObjectContext context, ObjectId id)`
  Returns an object matching ObjectId.
  `static Object`
  `objectForQuery(ObjectContext context, Query query)`
  Returns an object or a DataRow that is a result of a given query.
  `static Object`
  `pkForObject(Persistent object)`
  Returns a primary key value for a persistent object.
  `static Object`
  `readNestedProperty(Object o, String path)`
  Returns a value of the property identified by a property path.
  `static Object`
  `readNestedProperty(Object o, CayennePath path)`
  Returns a value of the property identified by a property path.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Cayenne

    protected Cayenne()

- ## Method Details

  - ### getObjEntity

    public static ObjEntity getObjEntity(Persistent p)
    Returns mapped ObjEntity for object. If an object is transient or is not mapped returns null.

  - ### getClassDescriptor

    public static ClassDescriptor getClassDescriptor(Persistent object)
    Returns class descriptor for the object or null if the object is not registered with an ObjectContext or descriptor was not found.

  - ### getProperty

    public static PropertyDescriptor getProperty(Persistent object, String propertyName)
    Returns property descriptor for specified property.
    Parameters:
      `propertyName` - path to the property
    Returns:
      property descriptor, `null` if not found

  - ### readNestedProperty

    public static Object readNestedProperty(Object o, String path)
    Returns a value of the property identified by a property path. Supports reading both mapped and unmapped properties. Unmapped properties are accessed in a manner consistent with JavaBeans specification.

    Property path (or nested property) is a dot-separated path used to traverse object relationships until the final object is found. If a null object found while traversing path, null is returned. If a list is encountered in the middle of the path, CayenneRuntimeException is thrown. Unlike `Persistent.readPropertyDirectly(String)`, this method will resolve an object if it is HOLLOW.

    Examples:

    - Read this object property:
      `String name = (String)Cayenne.readNestedProperty(artist, "name");`
    - Read an object related to this object:
      `Gallery g = (Gallery)Cayenne.readNestedProperty(paintingInfo, "toPainting.toGallery");`
    - Read a property of an object related to this object:
      `String name = (String)Cayenne.readNestedProperty(painting, "toArtist.artistName");`
    - Read to-many relationship list:
      `List exhibits = (List)Cayenne.readNestedProperty(painting, "toGallery.exhibitArray");`
    - Read to-many relationship in the middle of the path:
      `List<String> names = (List<String>)Cayenne.readNestedProperty(artist, "paintingArray.paintingName");`

  - ### readNestedProperty

    public static Object readNestedProperty(Object o, CayennePath path)
    Returns a value of the property identified by a property path. Supports reading both mapped and unmapped properties. Unmapped properties are accessed in a manner consistent with JavaBeans specification.

    Property path (or nested property) is a dot-separated path used to traverse object relationships until the final object is found. If a null object found while traversing path, null is returned. If a list is encountered in the middle of the path, CayenneRuntimeException is thrown. Unlike `Persistent.readPropertyDirectly(String)`, this method will resolve an object if it is HOLLOW.

    Since:
      5.0

  - ### makePath

    public static String makePath(String... pathParts)
    Constructs a dotted path from a list of strings. Useful for creating more complex paths while preserving compilation safety. For example, instead of saying:

        orderings.add(new Ordering("department.name", SortOrder.ASCENDING));

    You can use makePath() with the constants generated by Cayenne Modeler:

        orderings.add(new Ordering(Cayenne.makePath(USER.DEPARTMENT_PROPERTY, Department.NAME_PROPERTY), SortOrder.ASCENDING));

    Parameters:
      `pathParts` - The varargs list of paths to join.
    Returns:
      A string of all the paths joined by a "." (used by Cayenne in queries and orderings).

    Since:
      3.1

  - ### longPKForObject

    public static long longPKForObject(Persistent object)
    Returns an int primary key value for a persistent object. Only works for single column numeric primary keys. If an object is transient or has an ObjectId that can not be converted to an int PK, an exception is thrown.

  - ### intPKForObject

    public static int intPKForObject(Persistent object)
    Returns an int primary key value for a persistent object. Only works for single column numeric primary keys. If an object is transient or has an ObjectId that can not be converted to an int PK, an exception is thrown.

  - ### pkForObject

    public static Object pkForObject(Persistent object)
    Returns a primary key value for a persistent object. Only works for single column primary keys. If an object is transient or has a compound ObjectId, an exception is thrown.

  - ### compoundPKForObject

    public static Map<String,Object> compoundPKForObject(Persistent object)
    Returns a primary key map for a persistent object. This method is the most generic out of all methods for primary key retrieval. It will work for all possible types of primary keys. If an object is transient, an exception is thrown.

  - ### objectForPK

    public static <T> T objectForPK(ObjectContext context, Class<T> persistentObjectClass, int pk)
    Returns an object matching an int primary key. If the object is mapped to use non-integer PK or a compound PK, CayenneRuntimeException is thrown.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static <T> T objectForPK(ObjectContext context, Class<T> persistentObjectClass, Object pk)
    Returns an object matching an Object primary key. If the object is mapped to use a compound PK, CayenneRuntimeException is thrown.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static <T> T objectForPK(ObjectContext context, Class<T> persistantObjectClass, Map<String,?> pk)
    Returns an object matching a primary key. PK map parameter should use database PK column names as keys.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static Object objectForPK(ObjectContext context, String objEntityName, int pk)
    Returns an object matching an int primary key. If the object is mapped to use non-integer PK or a compound PK, CayenneRuntimeException is thrown.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static Object objectForPK(ObjectContext context, String objEntityName, Object pk)
    Returns an object matching an Object primary key. If the object is mapped to use a compound PK, CayenneRuntimeException is thrown.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static Object objectForPK(ObjectContext context, String objEntityName, Map<String,?> pk)
    Returns an object matching a primary key. PK map parameter should use database PK column names as keys.

    If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.

    See Also:
      - `objectForPK(ObjectContext, ObjectId)`

  - ### objectForPK

    public static Object objectForPK(ObjectContext context, ObjectId id)
    Returns an object matching ObjectId. If this object is already cached in the ObjectStore, it is returned without a query. Otherwise a query is built and executed against the database.
    Returns:
      A persistent object that matched the id, null if no matching objects were found
    Throws:
      `CayenneRuntimeException` - if more than one object matched ObjectId.

  - ### objectForQuery

    public static Object objectForQuery(ObjectContext context, Query query)
    Returns an object or a DataRow that is a result of a given query. If query returns more than one object, an exception is thrown. If query returns no objects, null is returned.