Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.cayenne
Class Cayenne

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

    public final class Cayenne

extends java.lang.Object

Various utils for processing persistent objects and their properties

DataObjects 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

Method Summary
`static java.util.Map<java.lang.String,java.lang.Object>` | `compoundPKForObject(Persistent dataObject)` 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 Property` | `getProperty(Persistent object, java.lang.String properyName)` Returns property descriptor for specified property.
`static int` | `intPKForObject(Persistent dataObject)` Returns an int primary key value for a persistent object.
`static long` | `longPKForObject(Persistent dataObject)` Returns an int primary key value for a persistent object.
`static java.lang.String` | `makePath(java.lang.String... pathParts)` Constructs a dotted path from a list of strings.
`static
`<T> T`
` | `objectForPK(ObjectContext context, java.lang.Class<T> dataObjectClass, int pk)` Returns an object matching an int primary key.
`static
`<T> T`
` | `objectForPK(ObjectContext context, java.lang.Class<T> dataObjectClass, java.util.Map<java.lang.String,?> pk)` Returns an object matching a primary key.
`static
`<T> T`
` | `objectForPK(ObjectContext context, java.lang.Class<T> dataObjectClass, java.lang.Object pk)` Returns an object matching an Object primary key.
`static java.lang.Object` | `objectForPK(ObjectContext context, ObjectId id)` Returns an object matching ObjectId.
`static java.lang.Object` | `objectForPK(ObjectContext context, java.lang.String objEntityName, int pk)` Returns an object matching an int primary key.
`static java.lang.Object` | `objectForPK(ObjectContext context, java.lang.String objEntityName, java.util.Map<java.lang.String,?> pk)` Returns an object matching a primary key.
`static java.lang.Object` | `objectForPK(ObjectContext context, java.lang.String objEntityName, java.lang.Object pk)` Returns an object matching an Object primary key.
`static java.lang.Object` | `objectForQuery(ObjectContext context, Query query)` Returns an object or a DataRow that is a result of a given query.
`static java.lang.Object` | `pkForObject(Persistent dataObject)` Returns a primary key value for a persistent object.
`static java.lang.Object` | `readNestedProperty(java.lang.Object o, java.lang.String path)` Returns a value of the property identified by a property path.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### 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 Property getProperty(Persistent object,
                                       java.lang.String properyName)

  Returns property descriptor for specified property.

  Parameters:
    `properyName` - path to the property
  Returns:
    property descriptor, `null` if not found

### readNestedProperty

    public static java.lang.Object readNestedProperty(java.lang.Object o,
                                                      java.lang.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 `#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 names = (List)Cayenne.readNestedProperty(artist, "paintingArray.paintingName");`

### makePath

    public static java.lang.String makePath(java.lang.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 dataObject)

  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 dataObject)

  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 java.lang.Object pkForObject(Persistent dataObject)

  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 java.util.Map<java.lang.String,java.lang.Object> compoundPKForObject(Persistent dataObject)

  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,
                                    java.lang.Class<T> dataObjectClass,
                                    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,
                                    java.lang.Class<T> dataObjectClass,
                                    java.lang.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,
                                    java.lang.Class<T> dataObjectClass,
                                    java.util.Map<java.lang.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 java.lang.Object objectForPK(ObjectContext context,
                                               java.lang.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 java.lang.Object objectForPK(ObjectContext context,
                                               java.lang.String objEntityName,
                                               java.lang.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 java.lang.Object objectForPK(ObjectContext context,
                                               java.lang.String objEntityName,
                                               java.util.Map<java.lang.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 java.lang.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 java.lang.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.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2012 Apache Cayenne. All Rights Reserved.