20

The NamingStrategy was already being marked as deprecated in Hibernate 4.2/4.3 (HHH-7079). Starting with Hibernate 5, now it will shipped with two replacements(?) interfaces ImplictNamingStrategy and PhysicalNamingStrategy (HHH-7078) and have finally ditched support for the old NamingStrategy. That's why Spring's upcoming Hibernate5 supported has even removed the configurer namingStrategy() and favor of implicitNamingStrategy() and physicalNamingStrategy(). So far, so good.

Although it is mentioned in some documents (i.e. in the incomplete working-5.0-migration-guide.md) and in (generated) release notes for the upcoming Hibernate 5 (as of today), I've found no concrete example how to use these actually.

Perhaps I've missed something, but neither the JavaDoc nor the issues shows any evidence of the idea both strategy types. Furthermore I've already one strategy based on NamingStrategy: a custom OracleNamingStrategy which is based on the well-known one.

While I'm interested in a proper solution in code (obviously), the actual issue is getting a working migration document and/or the conceptual idea of the restructured naming strategy.


Also related:

Community
  • 1
  • 1
knalli
  • 1,973
  • 19
  • 31
  • 4
    You've already mentioned _HHH-7078_ there is also a link to [HHH-9417](https://hibernate.atlassian.net/browse/HHH-9417) it describes the reason why they splitted the NamingStrategy. For the migration your existing `OracleNamingStrategy` must be modified so that it implements the `PhysicalNamingStrategy`. Was that your question? – andih Jul 11 '15 at 16:01
  • @andih Thank you. Well, is `PhysicalNamingStrategy` the "successor" of `NamingStrategy`? HHH-9417 explains the topic very shortly; yeah it could be helpful. I'm still not convinced about 2-3 lines in an issue of many (that's no documentation). – knalli Jul 11 '15 at 22:30
  • I would not call it successor. It depends what's the intention to provide a different naming strategy. If you want to control how implicit names are generated than you'll have to implement the `ImplicitNamingStrategy`. If your intention is to control how implicit/explicit ames are mapped to physical db tables/columns than you'll have to implement the `PhysicalNamingStrategy`. If you need more information about the how and why you'll have to search the dev mailing list. Feel free to help them to update the documentation - it's an open source project. Contributors are welcome. – andih Jul 12 '15 at 04:22

2 Answers2

2

Put below key value pair in your hibernate configuration file

hibernate.implicit_naming_strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl

hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Anil Agrawal
  • 2,748
  • 1
  • 24
  • 31
2
  1. If you are providing @Table and @Column annotation in your entity classes with names provided with an underscore i.e. user_id i.e. @Column(name="user_id"), it will take the column name as user_id; if you give it as userid then it will change to user_id if you use no strategy or implicit strategy (specifically spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl). So, if you want a strategy where the entity attribute name changes to one with underscore and lowercase letters i.e. something from userId to user_id, you should use implicit or no strategy (which actually uses implicit strategy).

  2. If you don't want your naming strategy to add an underscore to the column name or class name, then the strategy that you need to use would look like: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. The things that you provide in annotations @Table and @Column’s name attribute would remain as it is.

  3. If you don't want to provide annotations and want to manually handle the table name and column names, you should extend the class org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl and override the required methods. If you still use annotations for some of the cases here, remember the overridden methods will apply on the names written in those annotations.

    spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy

Raj Kundalia
  • 73
  • 2
  • 8