|
Adding some thoughts from discussions via ML and IRC.
It would be nice if there was a way for providing custom generator annotations such as @CreationTimestamp. Such a generator annotation type might be defined like this:
@Generated(
when = INSERT,
generator = MyCreationTimestampGenerator.class
}
public @interface MyCreationTimestamp {
TemporalType type();
String source default "vm";
}
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:
public interface CustomDynamicValueGenerator<V, A extends Annotation> extends DynamicValueGenerator<V> {
void initialize(A generatorAnnotation);
}
public class MyCreationTimestampGenerator implements DynamicValueGenerator<String, MyCreationTimestamp> {
TemporalType type;
void initialize(MyCreationTimestamp annotation) {
this.type = annotation.type();
}
String generateValue(Session session, Object owner) {
}
}
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:
@Modifier(when={INSERT, UPDATE})
private String lastModifier;
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.
|