Spring-boot: Allow beans to be used as Hibernate naming strategies

Created on 6 Sep 2014  路  15Comments  路  Source: spring-projects/spring-boot

Spring Boot already contains it's own SpringNamingStrategy jpa naming strategy.

In many projects all database objects require to have it's own prefix like abc_customers.

It would be nice if Boot's SpringNamingStrategy would support setting a prefix e.g. using a property like: spring.jpa.hibernate.naming-strategy.prefix=abc

enhancement

Most helpful comment

Rather than offering a configuration property that can be used to apply a table name prefix, and only when you're using Boot's default naming strategies, I'd prefer a more general purpose solution.

This branch contains a proposal that allows the user to provide ImplicitNamingStrategy and PhysicalNamingStrategy beans that are automatically applied to Hibernate by Boot's auto-configuration. In addition to providing more flexibility than a table name prefix property, using beans means that the naming strategies can be auto-configured by a library and can be customised or back-off as is appropriate for that library's needs.

All 15 comments

FWIW, since the class handling the naming strategy is loaded by Hibernate (and not by Spring Boot), the only way seems you have to code your own class (implementing org.hibernate.boot.model.naming.PhysicalNamingStrategy and/or org.hibernate.boot.model.naming.ImplicitNamingStrategy) then set the corresponding Spring Boot properties (spring.jpa.hibernate.naming.implicit-strategy and/or spring.jpa.hibernate.naming.physical-strategy)

The SpringPhysicalNamingStrategy class is now easier to subclass if you need to adapt our behavior. I'd rather not add a property for this, it feel like something that's more a fundamental part of your program rather than something you might want to configure differently at runtime. Adapting the strategy is probably good enough.

Re-opening since @jvalkeal has multiple places in dataflow/skipper/statemachine where this would be useful.

This all started when we realised that having an entity class named Release is causing use of reserved keyword with mysql. Making a custom naming strategy is always an option but then you need to place same boilerplate code in every project and then you kinda make mess with a relatively awkward configuration(as dataflow/skipper/statemachine components can be consumed as a libraries). With skipper we're still on milestone phase so we have an option to rename those entity classes but with existing stuff which is already released, you may not have a choice to rename your classes.

Being able to add a simple prefix to table names seem to be relatively easy way to make sure you're never going to get into trouble with used names with DB's. Having this simple prefix support in boot itself, seem to be the most cleanest way to provide this to end users. Also this would be good for cases where for some reason two different apps having a need to use a same DB schema, have a collisions with their DB naming.

For full blown end user app, it is be relatively easy to extend SpringNamingStrategy, but you could not do that with a library which boot itself depend on as then you'd have a circular dependency. Not an issue for dataflow/skipper/statemachine as boot doesn't currently consume those, but i.e. statemachine is something which might one day go to boot as currently it has its own spring-statemachine-boot module.

馃憤

@philwebb Did you actually forget to re-open this?

@jvalkeal Looks like it. Re-opened now.

It doesn't appear to be documented anywhere, but Hibernate supports the values of its hibernate.implicit_naming_strategy and hibernate.physical_naming_strategy properties being naming strategy instances as well as fully-qualified naming strategy class names. That, I think, gives us the hook point that we'd need to plug in naming strategies that apply a table prefix that's configurable via the environment.

Rather than offering a configuration property that can be used to apply a table name prefix, and only when you're using Boot's default naming strategies, I'd prefer a more general purpose solution.

This branch contains a proposal that allows the user to provide ImplicitNamingStrategy and PhysicalNamingStrategy beans that are automatically applied to Hibernate by Boot's auto-configuration. In addition to providing more flexibility than a table name prefix property, using beans means that the naming strategies can be auto-configured by a library and can be customised or back-off as is appropriate for that library's needs.

Fixed by 4bf1640198b439aa7e1e36ef9688ba12da08fe1c.

So I created a custom physical naming strategy by extending SpringPhysicalNamingStrategy and overriding a method.

I added the custom class in spring config:
jpa:
hibernate:
naming:
physical-strategy: com...*.NamingStrategy

My problem right now is that the custom "NamingStrategy" is not a spring bean and I cannot use for example @Value to add properties.

How can I use @Value in a custom NamingStrategy class ? Thanks !

@bardascat this issue is closed and we don't use the tracker for questions. Please ask on StackOverflow or come chat with the community on Gitter.

And before doing that, please take a moment to read the relevant section of the documentation.

Can you please tell me for which spring boot version was this feature added ? Thx v2.1.0.M1 ?

@bardascat The "milestone" at the top of the issue shows the version it was fixed in. In this case 2.0.0.M7.

Was this page helpful?
0 / 5 - 0 ratings