Set hibernate naming strategy as Spring bean -


i have spring-boot application hibernate. want provide custom namingstrategy add prefix every table managed hibernate.

i found can use property:

spring.jpa.hibernate.naming_strategy=com.whatever.mynamingstrategy 

this works fine except want have prefix dynamic based on properties. goal have like:

@component public class prefixnamingstrategy extends defaultnamingstrategy {      private final string prefix;      @autowired     public prefixnamingstrategy(@value("db.table.prefix") string prefix) {         this.prefix = prefix;     }      @override     public string tablename(string tablename) {         return prefix + super.tablename(tablename);     } } 

obviously doesn't work property.

i tried provide custom sessionfactoryand provide namingstrategy there had no luck - bean created after hibernate initialized , doesn't seem used:

@autowired @bean(name = "sessionfactory") public sessionfactory getsessionfactory(datasource datasource) {      localsessionfactorybuilder sessionbuilder = new localsessionfactorybuilder(datasource);      sessionbuilder.setnamingstrategy(...); // !      return sessionbuilder.buildsessionfactory(); } 

is there way how provide custom namingstrategy bean?

cheers

well knowing less ideal ended going through vendoradapter:

  1. the naming strategy delegator (note namingstrategy has been deprecated):

    @component public class prefixnamingstrategydelegator extends improvednamingstrategydelegator {  @autowired public prefixnamingstrategydelegator(final @value("${application.environment}_") string prefix) {     super(             new hbmnamingstrategydelegate() {                 @override                 public string tophysicaltablename(string tablename) {                     return prefix + super.tophysicaltablename(tablename);                 }             },             new jpanamingstrategydelegate() {                 @override                 public string tophysicaltablename(string tablename) {                     return prefix + super.tophysicaltablename(tablename);                 }             }     ); }  @override public namingstrategydelegate getnamingstrategydelegate(boolean ishbm) {     return super.getnamingstrategydelegate(ishbm); } } 
  2. the jpapersistenceprovider sneak namingstrategydelegator it:

    @component /**  * see {@link org.springframework.orm.jpa.vendor.springhibernatejpapersistenceprovider}  *  * can't subclass class still need sneak {@link org.hibernate.cfg.naming.namingstrategydelegator} it.  */ public class springhibernatejpapersistenceproviderwithnamingstrategy extends hibernatepersistenceprovider {  private final namingstrategydelegator namingstrategy;  @autowired public springhibernatejpapersistenceproviderwithnamingstrategy(namingstrategydelegator namingstrategy) {     this.namingstrategy = namingstrategy; }  @override public entitymanagerfactory createcontainerentitymanagerfactory(final persistenceunitinfo info, map properties) {     return new entitymanagerfactorybuilderimpl(new persistenceunitinfodescriptor(info), properties) {         @override         public configuration buildhibernateconfiguration(serviceregistry serviceregistry) {             configuration configuration = super.buildhibernateconfiguration(serviceregistry);             if (info instanceof smartpersistenceunitinfo) {                 (string managedpackage : ((smartpersistenceunitinfo) info).getmanagedpackages()) {                     configuration.addpackage(managedpackage);                 }             }              configuration.setnamingstrategydelegator(namingstrategy);              return configuration;         }     }.build(); } } 
  3. in @configuration:

    @bean public abstractjpavendoradapter customjpavendoradapter(hibernatepersistenceprovider jpapersistenceprovider) {     return new hibernatejpavendoradapter() {         @override         public persistenceprovider getpersistenceprovider() {             return jpapersistenceprovider;         }     }; } 

the persistenceprovider raw copy (its package private , can't sub classed) of springhibernatejpapersistenceprovider added line:

configuration.setnamingstrategydelegator(namingstrategy); 

works. ugly ... (you know)


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -