Yep, we could add an interface which allows the interception of *all*
lifecycle events.
interface LifecycleInterceptor {
DBObject prePersist(Object entity);
void postPersist(Object entity);
DBObject preLoad(DBObject dbObj, Object entity);
void postLoad(Object entity);
}
Where each method map one to one with the lifecycle annotations in entity class.
2010/5/10 Uwe Schäfer <
u...@thomas-daily.de>:
> Am 10.05.2010 18:14, schrieb Scott Hernandez:
>
> dear Scott
>
> i just checked out your latest work on EntityListener. thanks.
>
>> I don't see any reason you would have problems here, even now. Really,
>> the FTS stuff is logically outside your entity and stored as synthetic
>> properties that are derived from the actual entity data. I suspect
>> that you will only use those synthetic fields when issuing queries.
>
> right. that would be something like
>
> @PrePersist
> public DBObject PrePersistWithParamAndReturn(DBObject dbObj) {
> //...
> }
>
> right? by synthetic field, you mean that attributes are added to the
> returned DBObject, correct?
Yes, but in fact if you return null, or declare a void return then the
passed in DBObject is used. Maybe this is too complex since you will
probably only want to change the data in the dbObj and not replace the
reference. We can clean that up.
> i´d agree with that.
>
> problem is, how do i know the entity or its class ?
> i´d need to do some reflection in order to make something like
>
> @StemmedKeywords(indexName="title", stemmer=GERMAN,
> stopWordExclusion=GERMAN)
> private String germanTitle;
>
> happen, as well as maybe the actual entity object (would be handier, than
> the DBObject, i guess?).
Yep, reflection is your friend. At the moment you can actually play
with the MappedClass/MappedField instances to affect this. That is
another part of the extensions that I have on the list but haven't
coded yet. It would let you easily add annotations to the registration
(mapping metadata discovery) process where the morphia will inspect
(and do the reflection for you) and then you can define added
behavior. Right now you can do this by adding Classes to the statics
in those Mapped* classes.
Things are close but in another month they probably would have been
ready for things like this.
In your handler you can ask the mapper instance for all the
annotations that are relevant to the class you are working with.
> where am i wrong?
>
> the other thing: would there be a strong reason against making
> EntityListeners generally applicable with
>
> morphia.addEntityListener(IEntityListener el) ?
>
> The reason i´m asking is, it is very easy to use feature annotations like
> the above, but to fail registering the appropriate EntityListener.
> I´ve seen this in JPA hell of a lot, so that we ended up doing a prescan &
> validation of entity-class configurations.
I'm not sure what you mean, can you give me an example? Is it just
that you forget to add the @EntityListeners to the Entity class?
> cu & thx, uwe
>