Allow beans to be used as Hibernate naming strategies

Previously, custom Hibernate naming strategies could only be
configured via properties. This allowed a fully-qualified classname to
be specified, but did not allow a naming strategy instance to be used.

This commit updates HibernateJpaConfiguration to use
ImplicitNamingStrategy and PhysicalNamingStrategy beans if they
exist. If both a bean exists and the equivalent property has been set,
the bean wins.
This commit is contained in:
Andy Wilkinson
2017-11-22 11:44:57 +00:00
parent ffca60d308
commit 4bf1640198
7 changed files with 217 additions and 38 deletions

View File

@@ -1774,13 +1774,15 @@ Hibernate uses {hibernate-documentation}#naming[two different naming strategies]
names from the object model to the corresponding database names. The fully qualified
class name of the physical and the implicit strategy implementations can be configured by
setting the `spring.jpa.hibernate.naming.physical-strategy` and
`spring.jpa.hibernate.naming.implicit-strategy` properties, respectively.
`spring.jpa.hibernate.naming.implicit-strategy` properties, respectively. Alternatively,
if `ImplicitNamingStrategy` or `PhysicalNamingStrategy` beans are available in the
application context, Hibernate will be automatically configured to use them.
By default, Spring Boot configures the physical naming strategy with `SpringPhysicalNamingStrategy`.
This implementation provides the same table structure as Hibernate 4: all dots
are replaced by underscores and camel casing is replaced by underscores as well. By
default, all table names are generated in lower case, but it is possible to override that
flag if your schema requires it.
By default, Spring Boot configures the physical naming strategy with
`SpringPhysicalNamingStrategy`. This implementation provides the same table structure as
Hibernate 4: all dots are replaced by underscores and camel casing is replaced by
underscores as well. By default, all table names are generated in lower case, but it is
possible to override that flag if your schema requires it.
For example, a `TelephoneNumber` entity is mapped to the `telephone_number` table.
@@ -1791,6 +1793,15 @@ If you prefer to use Hibernate 5's default instead, set the following property:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
----
Alternatively, configure the following bean:
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Bean
public PhysicalNamingStrategy physicalNamingStrategy() {
return new PhysicalNamingStrategyStandardImpl();
}
----
See {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[`HibernateJpaAutoConfiguration`]
and {sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[`JpaBaseConfiguration`]