Adding some thoughts from discussions via ML and IRC.
It would be nice if there was a way for the user to declare custom generator annotations such as {{@CreationTimestamp}}. Such a _generator annotation type_ might be defined like this:
{code} @Generated( when = INSERT, generator = MyCreationTimestampGenerator.class } @Target({ METHOD, FIELD}) @Retention(RUNTIME) public @interface MyCreationTimestamp { TemporalType type(); String source default "vm"; } {code}
Here the {{@Generated}} annotation is used to mark {{@MyCreationTimestamp}} as custom generator annotation type, specifying the generator type and when to invoke it. A custom generator annotation may define additional attributes as suited per its requirements.
The generator of such an annotation would implement a specialized contract which receives the annotation instance upon initalization:
{code} public interface CustomDynamicValueGenerator<V, A extends Annotation> extends DynamicValueGenerator<V> { void initialize(A generatorAnnotation); } {code}
{code} public class MyCreationTimestampGenerator implements DynamicValueGenerator<String, MyCreationTimestamp> {
TemporalType type;
void initialize(MyCreationTimestamp annotation) { this.type = annotation.type(); }
String generateValue(Session session, Object owner) { //... } } {code}
Thinking about it some more, when given on a custom generator annotation type, {{@Generated#generator()}} should only accept a {{CustomDynamicValueGenerator}} type, so having a special annotation might be the better idea.
Optionally it might also make sense to allow for specifying the time of execution for custom generators:
{code} @Modifier(when={INSERT, UPDATE}) private String lastModifier; {code}
Unfortunately one can't define a contract for annotation types by means of an interface, so one could only declare a naming convention for this method.
|