Hibernate 5.1.x命名策略(向后兼容Hibernate 4.x) [英] Hibernate 5.1.x naming Strategy (backward compatible with Hibernate 4.x)
问题描述
我使用的是Spring Boot 1.3.3.RELEASE。默认情况下,Spring Boot使用Hibernate Version 4.x.我试图使用新的Hibernate ie 5.1.0 FINAL(截至目前)。
我使用Gradle来重写Hibernate Version我添加了以下行
ext ['hibernate.version'] =5.1.0.Final
按照 SpringBoot 1.3.0支持hibernate 5?
我使用以下命名策略
spring.jpa.properties.hibernate.naming.implicit策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.properties.hibernate.naming.physical_strategy:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
我有一个实体类
@Entity
公共类AppUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私人长ID;
@NotNull
@Length(max = 100)
私人字符串用户名;
@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)
私人字符串密码;
@NotNull
私有布尔启用;
在Hibernate 4.x上执行查询
create table app_user(
id bigint not null auto_increment,
email varchar(100)not null,
使能位不为空,
first_name varchar(100)不为null,
last_name varchar(100)不为空,
中间名varchar(100),
密码varchar(100)不是null,
username varchar(100)not null,
主键(id)
)
在5.x上执行查询
create table AppUser(
id bigint not null auto_increment,
email varchar(100)非空,
启用位不为空,
firstName varchar(100)非空,
lastName varchar(100)非空,
middleName varchar(100),
password varchar(100)not null,
username varchar(100)not null,
prima ry key(id)
)
如何设置命名策略,使Hibernate使用5.x在表名和列名上加下划线(如4.x)
首先,你不需要
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
因为它什么也不做, Hibernate作为默认设置。
Hibernate 5没有您想要的策略。所有策略都符合JPA(生成名称,如 AppUser
)。所以你需要实现自己的。
例如一个物理命名策略
公共类UnderscorePhysicalStartegy扩展PhysicalNamingStrategyStandardImpl {
@覆盖
公共标识符toPhysicalTableName(标识符名称,JdbcEnvironment上下文){
返回context.getIdentifierHelper()
。 toIdentifier(NamingStrategyUtils.classToName(name.getText()));
}
}
它使用 NamingStrategyUtils 。
请记住,如果您指定显式名称
@Entity
@Table(name =AppUser)
public class AppUser {
}
您将拥有一个表名 app_user
。如果您不想要这样的行为,请使用隐式命名策略。
我对命名策略进行了一些研究。你可以参考 这个类用于生成名称: HibernateNamingStrategy 。 如何使用 命名策略可以使用 StrategyOptions 。 例如,前缀(例如 其他示例: Hibernate 5隐式命名策略 除此之外, ImprovedNamingStrategy为休眠5 可用于模拟休眠的行为4 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). I'm using Gradle so to override the Hibernate Version I've added the following line followed the steps of SpringBoot 1.3.0 support hibernate 5? I'm using following for naming Strategy I've have a Entity class On Hibernate 4.x it executes the query on 5.x it executed the query How can I set the naming strategy such that Hibernate Uses 5.x underscore (as 4.x) on Table name and Column Name Firstly, you don't need
because of it does nothing and is used by Hibernate as default. Hibernate 5 doesn't have a strategy that you want. All strategies are JPA compliant (generate names like For an example a physical naming strategy It uses NamingStrategyUtils. Keep in mind, if you specify an explicit name you will have anyway a table name I do 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. This class is used to generate names: HibernateNamingStrategy. How to use The naming strategy can be configured using StrategyOptions. For example, to use strategy without the prefixes (like Other examples: Hibernate 5 Implicit Naming Strategy Except that, ImprovedNamingStrategy for Hibernate 5 can be used to simulate the behaviour of Hibernate 4 这篇关于Hibernate 5.1.x命名策略(向后兼容Hibernate 4.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Hibernate5NamingStrategy
f _
):
StrategyOptions options = StrategyOptions.builder ).withoutPrefixes()建立();
Hibernate5NamingStrategy strategy = new Hibernate5NamingStrategy(options);
ImprovedNamingStrategy
。 ext['hibernate.version']="5.1.0.Final"
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;
}
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)
)
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)
)
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
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()));
}
}
@Entity
@Table(name = "AppUser")
public class AppUser {
}
app_user
. If you don't want such behavior use an implicit naming strategy.Hibernate5NamingStrategy
f_
):StrategyOptions options = StrategyOptions.builder().withoutPrefixes().build();
Hibernate5NamingStrategy strategy = new Hibernate5NamingStrategy(options);
ImprovedNamingStrategy
.