Class PersistentObject
- All Implemented Interfaces:
- Serializable,- Persistent,- Validating
- Direct Known Subclasses:
- BaseDataObject,- GenericPersistentObject,- HybridPersistentObject
Persistent, have no assumption about how data is actually stored.
 Provides implementation of properties declared in Persistent interface.
 Three variants are currently supported:
- field based storage, e.g. each entity class will directly define fields to store data
-  Mapbased storage, e.g. values will be stored in general Map (GenericPersistentObject)
-  mixed fields and generic Map to store runtime attributes (HybridPersistentObject)
This class can be used directly as superclass for field-based data objects.
 To create own implementation of Persistent with custom field storage logic it is enough
 to implement readPropertyDirectly(String) and writePropertyDirectly(String, Object) methods
 and serialization support if needed (helper methods writeState(ObjectOutputStream)
 and readState(ObjectInputStream) are provided).
 
POJO Note
If having PersistentObject as a superclass presents a problem in an application, source code of this class can be copied verbatim to a custom class generation template. Desired superclass can be set in CayenneModeler.
- Since:
- 1.2
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected ObjectContextprotected ObjectIdprotected intprotected longFields inherited from interface org.apache.cayenne.PersistentDEFAULT_VERSION
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddToManyTarget(String relName, Persistent value, boolean setReverse) Adds an object to a to-many relationship.protected voidappendProperties(StringBuffer buffer) protected voidbeforePropertyRead(String propName) protected voidbeforePropertyWrite(String propName, Object oldValue, Object newValue) protected ObjectReturns a map key for a given to-many map relationship and a target object.intlongReturns a version of a DataRow snapshot that was used to create this object.readNestedProperty(String path) Returns a value of the property identified by a property path.Returns a value of the property identified by a property path.readProperty(String propertyName) Returns a value of the property identified by propName.readPropertyDirectly(String propName) Returns mapped property value as currently stored in the Persistent object.protected voidprotected voidvoidremoveToManyTarget(String relName, Persistent value, boolean setReverse) Removes an object from a to-many relationship.voidsetObjectContext(ObjectContext objectContext) voidsetObjectId(ObjectId objectId) voidsetPersistenceState(int persistenceState) protected voidsetReverseRelationship(String relName, Persistent val) Initializes reverse relationship from objectvalto this object.voidsetSnapshotVersion(long snapshotVersion) List<? extends Persistent>setToManyTarget(String relName, Collection<? extends Persistent> values, boolean setReverse) Sets the relationships to the specifiedPersistentobjects.voidsetToOneTarget(String relationshipName, Persistent value, boolean setReverse) Sets to-one relationship to a new value.toString()toStringBuffer(StringBuffer buffer, boolean fullDesc) A variation of "toString" method, that may be more efficient in some cases.protected voidunsetReverseRelationship(String relName, Persistent val) Removes current object from reverse relationship of objectvalto this object.voidvalidateForDelete(ValidationResult validationResult) This implementation does nothing.voidvalidateForInsert(ValidationResult validationResult) protected voidvalidateForSave(ValidationResult validationResult) Performs property validation of the object, appending any validation failures to the provided validationResult object.voidvalidateForUpdate(ValidationResult validationResult) protected voidwillConnect(String relationshipName, Persistent object) Called before establishing a relationship with another object.voidwriteProperty(String propName, Object val) Sets the property to the new value.voidwritePropertyDirectly(String propName, Object val) Modifies a value of a named property without altering the object state in any way, and without triggering any database operations.protected voidprotected void
- 
Field Details- 
objectId
- 
persistenceStateprotected int persistenceState
- 
objectContext
- 
snapshotVersionprotected long snapshotVersion
 
- 
- 
Constructor Details- 
PersistentObjectpublic PersistentObject()Creates a new transient object.
 
- 
- 
Method Details- 
getPersistenceStatepublic int getPersistenceState()- Specified by:
- getPersistenceStatein interface- Persistent
 
- 
getObjectContext- Specified by:
- getObjectContextin interface- Persistent
 
- 
setObjectContext- Specified by:
- setObjectContextin interface- Persistent
- Since:
- 1.2
 
- 
getObjectId- Specified by:
- getObjectIdin interface- Persistent
 
- 
setObjectId- Specified by:
- setObjectIdin interface- Persistent
 
- 
getMapKeyReturns a map key for a given to-many map relationship and a target object.- Since:
- 3.0
 
- 
readPropertyDirectlyDescription copied from interface:PersistentReturns mapped property value as currently stored in the Persistent object. Returned value maybe a fault or a real value. This method will not attempt to resolve faults, or to read unmapped properties.- Specified by:
- readPropertyDirectlyin interface- Persistent
 
- 
writePropertyDirectlyDescription copied from interface:PersistentModifies a value of a named property without altering the object state in any way, and without triggering any database operations. This method is intended mostly for internal use by Cayenne framework, and shouldn't be called from the application code.- Specified by:
- writePropertyDirectlyin interface- Persistent
 
- 
beforePropertyRead
- 
beforePropertyWrite
- 
readPropertyDescription copied from interface:PersistentReturns a value of the property identified by propName. Resolves faults if needed. This method can safely be used instead of or in addition to the auto-generated property accessors in subclasses of the Persistent object.- Specified by:
- readPropertyin interface- Persistent
 
- 
readNestedPropertyReturns 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)artist.readNestedProperty("name");
 
 
- Read an object related to this object:
 Gallery g = (Gallery)paintingInfo.readNestedProperty("toPainting.toGallery");
 
 
- Read a property of an object related to this object: 
 String name = (String)painting.readNestedProperty("toArtist.artistName");
 
 
- Read to-many relationship list:
 List exhibits = (List)painting.readNestedProperty("toGallery.exhibitArray");
 
 
- Read to-many relationship in the middle of the path:
 List<String> names = (List<String>)artist.readNestedProperty("paintingArray.paintingName");
 
 
 - Specified by:
- readNestedPropertyin interface- Persistent
- Since:
- 1.0.5
- See Also:
 
- Read this object property:
- 
readNestedPropertyDescription copied from interface:PersistentReturns 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)artist.readNestedProperty("name");
 
 
- Read an object related to this object:
 Gallery g = (Gallery)paintingInfo.readNestedProperty("toPainting.toGallery");
 
 
- Read a property of an object related to this object: 
 String name = (String)painting.readNestedProperty("toArtist.artistName");
 
 
- Read to-many relationship list:
 List exhibits = (List)painting.readNestedProperty("toGallery.exhibitArray");
 
 
- Read to-many relationship in the middle of the path:
 List<String> names = (List<String>)artist.readNestedProperty("paintingArray.paintingName");
 
 
 - Specified by:
- readNestedPropertyin interface- Persistent
- Parameters:
- path- a dot-separated path
- Returns:
- a value of the property identified by a property path
- See Also:
 
- Read this object property:
- 
writePropertyDescription copied from interface:PersistentSets the property to the new value. Resolves faults if needed. This method can be safely used instead of or in addition to the auto-generated property modifiers to set simple properties. Note that to set to-one relationships usePersistent.setToOneTarget(String, Persistent, boolean).- Specified by:
- writePropertyin interface- Persistent
- Parameters:
- propName- a name of the bean property being modified.
- val- a new value of the property.
 
- 
removeToManyTargetDescription copied from interface:PersistentRemoves an object from a to-many relationship.- Specified by:
- removeToManyTargetin interface- Persistent
 
- 
addToManyTargetDescription copied from interface:PersistentAdds an object to a to-many relationship.- Specified by:
- addToManyTargetin interface- Persistent
 
- 
setToManyTargetpublic List<? extends Persistent> setToManyTarget(String relName, Collection<? extends Persistent> values, boolean setReverse) Sets the relationships to the specifiedPersistentobjects.New relationships will be created with addToManyTarget(String, org.apache.cayenne.Persistent, boolean), already established relationships stay untouched. Missing relationships will be removed withremoveToManyTarget(String, org.apache.cayenne.Persistent, boolean)and returned as List. You may delete them manually.Notice: Moving an object relationship to another object, is still needing an manually "unregister" from the first object by removeToManyTarget(String, org.apache.cayenne.Persistent, boolean)- Parameters:
- relName- name of the relation
- values-- Persistentobjects of this- Collectionare set to the object. No changes will be made to the the- Collection, a copy is used. It is safe to pass a persisted- Collectionof another object.
- setReverse- update reverse relationships
- Returns:
- List<? extends Persistent>of unrelated Persistent objects. If no relationship was removed an empty List is returned.
- Throws:
- IllegalArgumentException- if no relationship could be read by relName, or if the passed- Collectionis null. To clear all relationships use an empty- Collection
- UnsupportedOperationException- if the relation Collection Type is neither- java.util.Collectionnor- java.util.Map
- Since:
- 4.0
 
- 
setToOneTargetDescription copied from interface:PersistentSets to-one relationship to a new value. Resolves faults if needed. This method can safely be used instead of or in addition to the auto-generated property modifiers to set properties that are to-one relationships.- Specified by:
- setToOneTargetin interface- Persistent
- Parameters:
- relationshipName- a name of the bean property being modified - same as the name of ObjRelationship.
- value- a new value of the property.
- setReverse- whether to update the reverse relationship pointing from the old and new values of the property to this object.
 
- 
willConnectCalled before establishing a relationship with another object. Applies "persistence by reachability" logic, pulling one of the two objects to a DataConext of another object in case one of the objects is transient. If both objects are persistent, and they don't have the same DataContext, CayenneRuntimeException is thrown.- Since:
- 1.2
 
- 
setReverseRelationshipInitializes reverse relationship from objectvalto this object.- Parameters:
- relName- name of relationship from this object to- val.
 
- 
unsetReverseRelationshipRemoves current object from reverse relationship of objectvalto this object.
- 
setPersistenceStatepublic void setPersistenceState(int persistenceState) - Specified by:
- setPersistenceStatein interface- Persistent
 
- 
getSnapshotVersionpublic long getSnapshotVersion()Description copied from interface:PersistentReturns a version of a DataRow snapshot that was used to create this object.- Specified by:
- getSnapshotVersionin interface- Persistent
- Since:
- 1.1
 
- 
setSnapshotVersionpublic void setSnapshotVersion(long snapshotVersion) - Specified by:
- setSnapshotVersionin interface- Persistent
- Since:
- 1.1
 
- 
validateForSavePerforms property validation of the object, appending any validation failures to the provided validationResult object. This method is invoked from "validateFor.." before committing a NEW or MODIFIED object to the database. Validation includes checking for null values and value sizes. PersistentObject subclasses may override this method, calling super.- Since:
- 1.1
 
- 
validateForInsertCallsvalidateForSave(ValidationResult). PersistentObject subclasses may override it providing validation logic that should be executed for the newly created objects before saving them.- Specified by:
- validateForInsertin interface- Validating
- Since:
- 1.1
 
- 
validateForUpdateCallsvalidateForSave(ValidationResult). PersistentObject subclasses may override it providing validation logic that should be executed for the modified objects before saving them.- Specified by:
- validateForUpdatein interface- Validating
- Since:
- 1.1
 
- 
validateForDeleteThis implementation does nothing. PersistentObject subclasses may override it providing validation logic that should be executed for the deleted objects before committing them.- Specified by:
- validateForDeletein interface- Validating
- Since:
- 1.1
 
- 
writeSerialized- Throws:
- IOException
 
- 
readSerialized- Throws:
- IOException
- ClassNotFoundException
 
- 
writeState- Throws:
- IOException
 
- 
readState- Throws:
- IOException
- ClassNotFoundException
 
- 
toStringBufferA variation of "toString" method, that may be more efficient in some cases. For example when printing a list of objects into the same String.
- 
appendProperties
- 
toString
 
-