*My context:* I work in a multi-tenant application with schema based segmentation. I also use the same entity classes in multiple applications.
I would like to define programmatically the schema to be used to locate each table or sequence.
The annotations related to schema are : {code:java} @Table(schema="myschema" ) @SequenceGenerator(sequenceName = "myschema.seq") @JoinTable(schema="myschema") {code}
For some annotations, I cannot define the schema statically. I would like Hibernate to use a strategy interface with this simple contract :
*Contract for internal Hibernate Code:* {code:java} //called each time the processing class has changed public void setCurrentProcessingClass(XClass xClass); //ask for dynamic schema name for table public String resolveSchemaName(String annotationSchemaName, String annotationTableName); //ask for dynamic schema name for sequence public String resolveSequenceName(String annotationSequenceName) {code}
I made call to this methods in two classes : - org.hibernate.cfg.AnnotationBinder {color:red} *( only 4 lines added)* {color} - org.hibernate.boot.internal.IdGeneratorInterpreterImpl *{color:red} ( only 1 line added) {color}*
Then I delegate to my application class with a simple interface.
*Contract provided by my application:* {code:java} String getSchema(Class<?> clazz) {code}
*I also can implement easily this services for schema filtering :* {code:java} public String getSchemaForTable(String tableName) public String getSchemaForSequence(String sequenceName) {code}
I can implement a SchemaFilterProvider with a SchemaFilter delegating on these methods:
*Hibernate org.hibernate.tool.schema.spi.SchemaFilter official contract:* {code:java} boolean includeTable(Table table); boolean includeSequence(Sequence sequence); {code}
Otherwise, the filter could provide schema name for update but I think this approach would be very complex taking into account the existing code...
* If I send you the code {color:red}(only 5 lines added in 2 files){color} , could it be considered to be integrated for a future release ? *
Thanks,
Benoit. |
|