1. Introduction
1.介绍
In this brief tutorial, we’ll see how to use the Hibernate naming strategies in a Spring Boot application.
在这个简短的教程中,我们将看到如何在Hibernate命名策略中使用Spring Boot应用程序。
2. Maven Dependencies
2.Maven的依赖性
If we begin with a Maven-based Spring Boot application, and we are happy to embrace Spring Data, then we just need to add the Spring Data JPA dependency:
如果我们从基于Maven的Spring Boot应用程序开始,并且乐于接受Spring Data,那么我们只需添加Spring Data JPA依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Also, we need a database, so we’ll use the in-memory database H2:
另外,我们需要一个数据库,所以我们将使用内存数据库H2。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
The Spring Data JPA dependency brings in the Hibernate dependencies for us.
Spring Data JPA的依赖性为我们带来了Hibernate的依赖性。
3. Spring Boot Naming Strategies
3.Spring Boot的命名策略
Hibernate maps field names using a physical strategy and an implicit strategy. We previously talked about how to work with the naming strategies in this tutorial.
Hibernate 使用物理策略和隐式策略映射字段名。我们之前在这个教程中谈到了如何使用命名策略。
And, Spring Boot, provides defaults for both:
而且,Spring Boot为这两者提供了默认值。
- spring.jpa.hibernate.naming.physical-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy, and
- spring.jpa.hibernate.naming.implicit-strategy defaults to org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
We can override these values, but by default, these will:
我们可以覆盖这些值,但在默认情况下,这些会。
- Replace dots with underscores
- Change camel case to snake case, and
- Lower-case table names
So, for example, an AddressBook entity would be created as the address_book table.
因此,例如,一个AddressBook实体将被创建为address_book表。
4. Strategies in Action
4.行动中的战略
If we create an Account entity:
如果我们创建一个Account实体。
@Entity
public class Account {
@Id
private Long id;
private String defaultEmail;
}
And then turn on some SQL debugging in our properties file:
然后在我们的属性文件中打开一些SQL调试功能。
hibernate.show_sql: true
At startup, we’ll see the following create statement in our logs:
在启动时,我们会在日志中看到以下create语句。
Hibernate: create table account (id bigint not null, default_email varchar(255))
As we can see, the fields use snake case and are lowercased, following the Spring conventions.
我们可以看到,字段使用蛇形大小写,并且是小写的,遵循Spring的惯例。
And remember that we don’t need to specify the physical-strategy or the implicit-strategy properties in this case since we are accepting the defaults.
记住,在这种情况下,我们不需要指定物理策略或隐性策略属性,因为我们接受默认值。。
5. Customizing Strategies
5.定制策略
So, let’s say that we want to use the strategies from JPA 1.0.
所以,假设我们想使用JPA 1.0的策略。
We only need to indicate it in our properties file:
我们只需要在我们的属性文件中注明。
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
Or, expose them as @Beans:
或者,将它们暴露为@Beans。
@Bean
public PhysicalNamingStrategy physical() {
return new PhysicalNamingStrategyStandardImpl();
}
@Bean
public ImplicitNamingStrategy implicit() {
return new ImplicitNamingStrategyLegacyJpaImpl();
}
Either way, if we run our example with these changes, we’ll see a slightly different DDL statement:
不管怎么说,如果我们用这些变化来运行我们的例子,我们会看到一个略有不同的DDL语句。
Hibernate: create table Account (id bigint not null, defaultEmail varchar(255), primary key (id))
As we can see, this time these strategies follow the JPA 1.0’s naming convention.
我们可以看到,这一次这些策略遵循了JPA 1.0的命名惯例。
Now, if we wanted to use the JPA 2.0 naming rules we’d need to set ImplicitNamingStrategyJpaCompliantImpl as our implicit strategy.
现在,如果我们想使用JPA 2.0的命名规则,我们需要将ImplicitNamingStrategyJpaCompliantImpl作为我们的隐式策略。。
6. Conclusion
6.结论
In this tutorial, we saw how Spring Boot applies naming strategies to Hibernate’s query generator, and we also saw how to customize those strategies.
在本教程中,我们看到Spring Boot如何将命名策略应用于Hibernate的查询生成器,我们还看到如何定制这些策略。
And, as always, make sure check out all these samples over on GitHub.
而且,像往常一样,请确保在GitHub上查看所有这些样本。