Class Cayenne
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 -
Method Summary
Modifier and TypeMethodDescriptioncompoundPKForObject(Persistent object) Returns a primary key map for a persistent object.static ClassDescriptorgetClassDescriptor(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 ObjEntityReturns mapped ObjEntity for object.static PropertyDescriptorgetProperty(Persistent object, String propertyName) Returns property descriptor for specified property.static intintPKForObject(Persistent object) Returns an int primary key value for a persistent object.static longlongPKForObject(Persistent object) Returns an int primary key value for a persistent object.static StringConstructs a dotted path from a list of strings.static <T extends Persistent>
TobjectForPK(ObjectContext context, Class<T> persistentObjectClass, int pk) Deprecated, for removal: This API element is subject to removal in a future version.static <T extends Persistent>
TobjectForPK(ObjectContext context, Class<T> persistentObjectClass, Object pk) Deprecated, for removal: This API element is subject to removal in a future version.static <T extends Persistent>
TobjectForPK(ObjectContext context, Class<T> persistentObjectClass, Map<String, ?> pk) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectobjectForPK(ObjectContext context, String objEntityName, int pk) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectobjectForPK(ObjectContext context, String objEntityName, Object pk) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectobjectForPK(ObjectContext context, String objEntityName, Map<String, ?> pk) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectobjectForPK(ObjectContext context, ObjectId id) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectobjectForQuery(ObjectContext context, Query query) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectpkForObject(Persistent object) Returns a primary key value for a persistent object.static ObjectreadNestedProperty(Object o, String path) Returns a value of the property identified by a property path.static ObjectreadNestedProperty(Object o, CayennePath path) Returns a value of the property identified by a property path.
-
Constructor Details
-
Cayenne
protected Cayenne()
-
-
Method Details
-
getObjEntity
Returns mapped ObjEntity for object. If an object is transient or is not mapped returns null. -
getClassDescriptor
Returns class descriptor for the object or null if the object is not registered with an ObjectContext or descriptor was not found. -
getProperty
Returns property descriptor for specified property.- Parameters:
propertyName- path to the property- Returns:
- property descriptor,
nullif not found
-
readNestedProperty
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");
- Read this object property:
-
readNestedProperty
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
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
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
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
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
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
@Deprecated(since="5.0", forRemoval=true) public static <T extends Persistent> T objectForPK(ObjectContext context, Class<T> persistentObjectClass, int pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static <T extends Persistent> T objectForPK(ObjectContext context, Class<T> persistentObjectClass, Object pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static <T extends Persistent> T objectForPK(ObjectContext context, Class<T> persistentObjectClass, Map<String, ?> pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static Object objectForPK(ObjectContext context, String objEntityName, int pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static Object objectForPK(ObjectContext context, String objEntityName, Object pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static Object objectForPK(ObjectContext context, String objEntityName, Map<String, ?> pk) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForPK
@Deprecated(since="5.0", forRemoval=true) public static Object objectForPK(ObjectContext context, ObjectId id) Deprecated, for removal: This API element is subject to removal in a future version. -
objectForQuery
@Deprecated(since="5.0", forRemoval=true) public static Object objectForQuery(ObjectContext context, Query query) Deprecated, for removal: This API element is subject to removal in a future version.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.
-
ObjectContext.objectForPK(Class, Object)