I have a spring-boot application with Hibernate. I want to provide custom NamingStrategy
to add prefix to every table managed by hibernate.
I found I can use property:
spring.jpa.hibernate.naming_strategy=com.whatever.MyNamingStrategy
This works fine except I want to have the prefix dynamic based on properties. My goal would be to have something 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 this doesn't work with the property.
I tried to provide custom SessionFactory
and provide the NamingStrategy
there but had no luck - the bean was created after hibernate initialized and even doesn't seem to be used:
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.setNamingStrategy(...); // !
return sessionBuilder.buildSessionFactory();
}
Is there a way how to provide custom NamingStrategy
as bean?
Cheers