Hibernate 5.1.x 命名策略(向后兼容 Hibernate 4.x) [英] Hibernate 5.1.x naming Strategy (backward compatible with Hibernate 4.x)

查看:16
本文介绍了Hibernate 5.1.x 命名策略(向后兼容 Hibernate 4.x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring Boot 1.3.3.RELEASE.默认情况下,Spring Boot 使用 Hibernate 版本 4.x.我正在尝试使用新的 Hibernate,即 5.1.0 FINAL(截至目前).

I'm using Spring Boot 1.3.3.RELEASE. By default Spring Boot uses the Hibernate Version 4.x. I'm trying to use new Hibernate i.e 5.1.0 FINAL (as of now).

我正在使用 Gradle 来覆盖 Hibernate 版本,我添加了以下行

I'm using Gradle so to override the Hibernate Version I've added the following line

ext['hibernate.version']="5.1.0.Final"

按照SpringBoot 1.3.0支持hibernate 5?

我使用以下命名策略

spring.jpa.properties.hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl

spring.jpa.properties.hibernate.naming.physical_strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

我有一个实体类

@Entity
public class AppUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Length(max = 100)
    private String username;

    @NotNull
    @Length(max = 100)
    private String firstName;

    @NotNull
    @Length(max = 100)
    private String lastName;

    @Length(max = 100)
    private String middleName;

    @NotNull
    @Length(max=100)
    private String email;

    @NotNull
    @Length(max = 100)
    private String password;

    @NotNull
    private boolean enabled;

}

在 Hibernate 4.x 上执行查询

On Hibernate 4.x it executes the query

create table app_user (
        id bigint not null auto_increment,
        email varchar(100) not null,
        enabled bit not null,
        first_name varchar(100) not null,
        last_name varchar(100) not null,
        middle_name varchar(100),
        password varchar(100) not null,
        username varchar(100) not null,
        primary key (id)
    )

在 5.x 上它执行了查询

on 5.x it executed the query

create table AppUser (
        id bigint not null auto_increment,
        email varchar(100) not null,
        enabled bit not null,
        firstName varchar(100) not null,
        lastName varchar(100) not null,
        middleName varchar(100),
        password varchar(100) not null,
        username varchar(100) not null,
        primary key (id)
    )

如何设置命名策略,使 Hibernate 在表名和列名上使用 5.x 下划线(如 4.x)

How can I set the naming strategy such that Hibernate Uses 5.x underscore (as 4.x) on Table name and Column Name

推荐答案

首先,你不需要org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

因为它什么都不做,并且被 Hibernate 默认使用.

because of it does nothing and is used by Hibernate as default.

Hibernate 5 没有您想要的策略.所有策略都符合 JPA(生成像 AppUser 这样的名称).所以你需要自己实现.

Hibernate 5 doesn't have a strategy that you want. All strategies are JPA compliant (generate names like AppUser). So you need to implement your own.

以物理命名策略为例

public class UnderscorePhysicalStartegy extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        return context.getIdentifierHelper()
                .toIdentifier(NamingStrategyUtils.classToName(name.getText()));
    }

}

它使用 NamingStrategyUtils.

请记住,如果您指定显式名称

Keep in mind, if you specify an explicit name

@Entity
@Table(name = "AppUser")
public class AppUser {

}

无论如何你都会有一个表名app_user.如果您不希望出现此类行为,请使用隐式命名策略.

you will have anyway a table name app_user. If you don't want such behavior use an implicit naming strategy.

我在命名策略方面做了一些研究工作.你可以参考 Hibernate5NamingStrategy,它生成带有下划线的表名和列名,就像您需要的那样,以及约束名称(唯一的,外键).

I did some research work on naming strategies. You can refer Hibernate5NamingStrategy, it generates table and column names with underscores like you need and constraint names (unique, foreign key) as well.

这个类用于生成名称:HibernateNamingStrategy.

This class is used to generate names: HibernateNamingStrategy.

如何使用Hibernate5NamingStrategy

命名策略可以使用StrategyOptions.

例如,使用没有前缀的策略(如f_):

For example, to use strategy without the prefixes (like f_):

StrategyOptions options = StrategyOptions.builder().withoutPrefixes().build();
Hibernate5NamingStrategy strategy = new Hibernate5NamingStrategy(options);

其他示例:Hibernate 5 隐式命名策略

除此之外,Hibernate 5 的改进命名策略可用于模拟 Hibernate 4 ImprovedNamingStrategy 的行为.

Except that, ImprovedNamingStrategy for Hibernate 5 can be used to simulate the behaviour of Hibernate 4 ImprovedNamingStrategy.

这篇关于Hibernate 5.1.x 命名策略(向后兼容 Hibernate 4.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆