I worked on 3) and I got to something apparently working.
I'm still a little bit worried, I worked my way by reading more code than documentation, So I'm not quite sure it is a nice way to do it or a bad hack
that would break with next version.
I am also still wondering if there isn't any other way to implement discriminatorType with annotations.
I past the code there in case it is any use to someone. (although I mean to be speaking to myself here)
Attention: you need to use cfg.buildEntityManagerFactory() after that
and nor Persistence.createEntityManagerFactory() otherwise you'll work with another Factory.
Code:
org.hibernate.ejb.Ejb3Configuration cfg=new org.hibernate.ejb.Ejb3Configuration();
cfg.configure("myconf",null);
debug(ConfigurationEnhancer.class," configure done ");
Mappings mappings=cfg.getHibernateConfiguration().createMappings();
for(Iterator it=cfg.getClassMappings();it.hasNext();){
PersistentClass classe=(PersistentClass) it.next();
debug(ConfigurationEnhancer.class,"checking ",classe," for Discrimator");
SimpleValue value=(SimpleValue)classe.getDiscriminator();
if(value!=null){
String name=classe.getClassName();
try{
debug(ConfigurationEnhancer.class,"checking ",name," for user DiscrimatorType");
Class maClasse=Class.forName(name);
if(maClasse.isAnnotationPresent(DiscriminatorType.class)){
DiscriminatorType type=(DiscriminatorType) maClasse.getAnnotation(DiscriminatorType.class);
String typeName=type.type();
debug(ConfigurationEnhancer.class,"annotation present in",name,"type",typeName);
TypeDef typeDef=mappings.getTypeDef(typeName);
if(typeDef!=null){
debug(ConfigurationEnhancer.class,"typedef found for",typeName);
typeName=typeDef.getTypeClass();
// parameters on the property mapping should
// override parameters in the typedef
Properties parameters = new Properties();
parameters.putAll( typeDef.getParameters() );
if ( !parameters.isEmpty() ) value.setTypeParameters( parameters );
}
debug(ConfigurationEnhancer.class,"set type",typeName);
if ( typeName != null ) value.setTypeName( typeName );
}
}
catch(ClassNotFoundException e){
staticError(ConfigurationEnhancer.class,e);
}
}
}