我正在探索子类化 AbstractCouchbaseEventListener
的选项,以包括自定义文档/实体生命周期操作 . 我对Spring很新,所以我意识到这可能是一种次优的方法(如果是,那么Spring的方式会更好或更常见?) .
目前,我正在考虑为实体保存/删除时设置/删除参考文档 . 对于"reference document"目前的用途,我的意思是将特定实体类唯一键值映射到包含它的文档的文档:如果 User.username
是"billyTheKid"且 User.id
是12345,则文档 User:by:username:12345
包含12345,因此用作指向用户文档的指针 .
我目前的方法是有一个接口 ReferenceDocumentOwner
,可以由任何想要存储引用文档的实体类实现:
public interface ReferenceDocumentOwner<T, R> extends BasicDocument {
/*
Store the existing (stored) version of the entity. This is called in onBeforeSave()
to populate the about-to-be saved instance with its stored version. This previous
version can be used to determine the fields and values to populate the returns of
getDeleteRefDocMap() and getStoreRefDocMap().
*/
public void storePreviousRecord(T recorded);
public Class<R> getRepositoryClass();
/*
For values that used to be non-empty but are now empty. For example, if the
concrete implementation of this interface was User.class and this method returned:
electricServiceId => 12345,
garbageServiceId => AKIE3423
The reference documents {@code User:by:electricServiceId:12345} and
{@code User:by:garbageServiceId:AKIE3423} would be deleted in onAfterSave().
*/
public Map<String, Object> getDeleteRefDocMap ();
/*
For values that are non-empty. For example, if the concrete implementation of this
interface was User.class and this method returned:
email => someEmailValue,
garbageServiceId => AKIE3423,
username => billyTheKid
The reference documents {@code User:by:email:someEmailValue},
{@code User:by:garbageServiceId:AKIE3423}, and {@code User:by:username:billyTheKid}
would be stored pointing to the User instance's {@code id} in onAfterSave().
*/
public Map<String, Object> getStoreRefDocMap ();
}
( BasicDocument
这里只是另一个定义 getId()
和 setId()
的简单接口 . )
我有一个 ReferenceDocumentOwnerSaveDeleteEventListener extends AbstractCouchbaseEventListener<ReferenceDocumentOwner>
实现来管理所有这些 . 我知道onBeforeSave()和onAfterSave()之间存在潜在的竞争条件,并且为了这次探索的目的,一旦存储了这些参考文档,删除它们的"owning"文档时没有简单的方法来删除它们 . onBeforeDelete()/ onAfterDelete()方法只接收 CouchbaseDocument
,而不是源对象 .
问题:为什么这些删除事件不像其他事件处理程序那样传递源对象?
问题:人们是否有推荐的方式/其他方式来实施参考文件?
编辑:
另一个问题是 onBeforeDelete()
和 onAfterDelete()
被传递 null
而不是被删除的文件,即使 repository.delete()
操作成功 .
1 回答
回答我自己的问题(直到其他人可以更好地澄清):
进一步深入到couchbase事件代码中,看起来删除相关事件对象永远不会有CouchbaseDocument,因为它们是使用源对象创建的,但是使用
null
document参数调用它们的超类CouchbaseMappingEvent
构造函数 . 我发布了pull request that fixes this .