|
Consider the pattern used for org.hibernate.annotations.ValueGenerationType.
In some ways this is an extension to GenericGenerator. One option is to define this annotation with the intention that it be attached to the custom org.hibernate.id.IdentifierGenerator implementation class for discovery and some basic configuration. E.g.
@ThisNewAnnotation
public class MyCustomGenerator implements IdentifierGenerator {
...
}
Like we did with ValueGenerationType, I would love for this to express how the implementation should be configured from annotation values. Not sure exactly how this would look, but something like:
/**
* Annotation to identify IdentifierGenerator implementations
*/
public @interface GeneratorImplementation {
Class[] generatorImpls();
}
/**
* Extended IdentifierGenerator contract to account for annotation consumption
*/
public interface AnnotationBasedIdentifierGenerator<A extends Annotation,T extends Serializable>
extends IdentifierGenerator {
/**
* Consume the annotation (configuration)
*/
void initialize(A ann);
@Override
T generate(SessionImplementor session, Object object);
}
/**
* My custom generator annotation
*/
public interface @UuidGenerator {
enum Strategy( RANDOM, RFC_4122 )
String name();
Strategy strategy() default RFC_4122;
}
@GeneratorImplementation
public class UuidIdentifierGenerator
implements AnnotationBasedIdentifierGenerator<UuidGenerator,UUID> {
private String generatorName;
private UUIDGenerationStrategy uuidGenStrategy;
@Override
public void initialize(UuidGenerator ann) {
this.generatorName = ann.name();
this.uuidGenStrategy = decodeStrategy( ann.strategy() );
}
private UUIDGenerationStrategy decodeStrategy(Strategy strategy) {
switch( strategy ) {
...
}
}
@Override
UUID generate(SessionImplementor session, Object object) {
return uuidGenStrategy.generateUUID( session );
}
}
public class MyEntity {
@GeneratedValue
@UuidGenerator(strategy=RANDOM)
private UUID id;
...
}
|