Interface ObjectRepository<T>
- Type Parameters:
T- the type of the object to store.
- All Superinterfaces:
org.dizitart.no2.common.meta.AttributesAware,AutoCloseable,EventAware,PersistentCollection<T>
NitriteCollection, where all objects are converted
into a Document and saved into the database.
An object repository is observable like its underlying NitriteCollection.
// create/open a database
Nitrite db = Nitrite.builder()
.openOrCreate("user", "password");
// create an object repository
ObjectRepository<Employee> employeeStore = db.getRepository(Employee.class);
// insert an object
Employee emp = new Employee();
emp.setName("John Doe");
employeeStore.insert(emp);
- Since:
- 1.0
- Author:
- Anindya Chatterjee.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionfind()Returns a cursor to all objects in the collection.find(FindOptions findOptions) Returns a customized cursor to all objects in the collection.Applies a filter on the collection and returns a cursor to the selected objects.find(Filter filter, FindOptions findOptions) Applies a filter on the collection and returns a customized cursor to the selected objects.<I> TgetById(I id) Gets a single element from the repository by its id.Returns the underlyingNitriteCollectioninstance.getType()Returns the type associated with theObjectRepository.default WriteResultInserts object into this repository.default WriteResultRemoves matching elements from the collection.Removes object from the collection.default WriteResultUpdates object in the repository by setting the field specified indocument.Updates object in the repository by setting the field specified indocument.default WriteResultUpdates object in the repository.update(Filter filter, T update, UpdateOptions updateOptions) Updates object in the repository.Methods inherited from interface org.dizitart.no2.common.meta.AttributesAware
getAttributes, setAttributesMethods inherited from interface org.dizitart.no2.collection.events.EventAware
subscribe, unsubscribeMethods inherited from interface org.dizitart.no2.common.PersistentCollection
addProcessor, clear, close, createIndex, createIndex, drop, dropAllIndices, dropIndex, getStore, hasIndex, insert, isDropped, isIndexing, isOpen, listIndices, rebuildIndex, remove, size, update, update
-
Method Details
-
insert
Inserts object into this repository. If the object contains a value marked withId, then the value will be used as a unique key to identify the object in the repository.If any of the value is already indexed in the repository, then after insertion the index will also be updated.
NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Insert.- Parameters:
object- the object to insertothers- other objects to insert in a batch.- Returns:
- the result of the write operation.
- Throws:
ValidationException- ifobjectisnull.InvalidIdException- if the id value containsnullvalue.InvalidIdException- if the id value contains non comparable type, i.e. type that does not implementComparable.InvalidIdException- if the id contains value which is not of the same java type as of other objects' id in the collection.UniqueConstraintException- if the value of id value clashes with the id of another object in the collection.UniqueConstraintException- if a value of the object is indexed, and it violates the unique constraint in the collection(if any).- See Also:
-
update
Updates object in the repository. If the filter does not find any object in the collection, then theupdateobject will be inserted.If the
filterisnull, it will update all objects in the collection.CAUTION: If the
updateobject has a nonnullvalue in the id value, this value will be removed before update.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Update.- Parameters:
filter- the filter to apply to select objects from the collection.update- the modifications to apply.- Returns:
- the result of the update operation.
- Throws:
ValidationException- if theupdateobject isnull.
-
update
Updates object in the repository. Update operation can be customized with the help ofupdateOptions.If the
filterisnull, it will update all objects in the collection unlessjustOnceis set totrueinupdateOptions.CAUTION: If the
updateobject has a nonnullvalue in the id value, this value will be removed before update.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.UpdateorEventType.Insert.- Parameters:
filter- the filter to apply to select objects from the collection.update- the modifications to apply.updateOptions- various options for update operation.- Returns:
- the result of the update operation.
- Throws:
ValidationException- if theupdateobject isnull.ValidationException- ifupdateOptionsisnull.
-
update
Updates object in the repository by setting the field specified indocument.If the
filterisnull, it will update all objects in the collection.CAUTION: The
updatedocument should not contain_idfield.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Update.- Parameters:
filter- the filter to apply to select objects from the collection.update- the modifications to apply.- Returns:
- the result of the update operation.
- Throws:
ValidationException- if theupdateobject isnull.
-
update
Updates object in the repository by setting the field specified indocument. Update operation can either update the first matching object or all matching objects depending on the value ofjustOnce.If the
filterisnull, it will update all objects in the collection unlessjustOnceis set totrue.CAUTION: The
updatedocument should not contain_idfield.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Update.- Parameters:
filter- the filter to apply to select objects from the collection.update- the modifications to apply.justOnce- indicates if update should be applied on first matching object or all.- Returns:
- the result of the update operation.
- Throws:
ValidationException- if theupdateobject isnull.
-
remove
Removes matching elements from the collection.If the
filterisnull, it will remove all objects from the collection.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Remove.- Parameters:
filter- the filter to apply to select elements from collection.- Returns:
- the result of the remove operation.
-
remove
Removes object from the collection. Remove operation can be customized byremoveOptions.If the
filterisnull, it will remove all objects in the collection unlessjustOnceis set totrueinremoveOptions.NOTE: This operations will notify all
CollectionEventListenerinstances registered to this collection with change typeEventType.Remove.- Parameters:
filter- the filter to apply to select objects from collection.justOne- indicates if only one element will be removed or all of them.- Returns:
- the result of the remove operation.
-
find
Returns a cursor to all objects in the collection.- Returns:
- a cursor to all objects in the collection.
-
find
Applies a filter on the collection and returns a cursor to the selected objects.See
Filterfor all available filters.NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.
- Parameters:
filter- the filter to apply to select objects from collection.- Returns:
- a cursor to all selected objects.
- See Also:
-
find
Returns a customized cursor to all objects in the collection.- Parameters:
findOptions- specifies pagination, sort options for the cursor.- Returns:
- a cursor to all selected objects.
-
find
Applies a filter on the collection and returns a customized cursor to the selected objects.NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.
- Parameters:
filter- the filter to apply to select objects from collection.findOptions- specifies pagination, sort options for the cursor.- Returns:
- a cursor to all selected objects.
-
getById
Gets a single element from the repository by its id. If no element is found, it will returnnull. The object must have a field annotated withId, otherwise this call will throwInvalidIdException.- Type Parameters:
I- the type parameter- Parameters:
id- the id value- Returns:
- the unique object associated with the id.
- Throws:
ValidationException- ifidisnull.InvalidIdException- if the id value isnull, or the type is not compatible.NotIdentifiableException- if the object has no field marked withId.
-
getType
Returns the type associated with theObjectRepository.- Returns:
- type of the object.
-
getDocumentCollection
NitriteCollection getDocumentCollection()Returns the underlyingNitriteCollectioninstance.- Returns:
- the underlying document collection.
-