Hibernate 5.1.x命名策略(向后兼容Hibernate 4.x)

编程入门 行业动态 更新时间:2024-10-26 04:19:25
本文介绍了Hibernate 5.1.x命名策略(向后兼容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 。

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

您将拥有一个表名 app_user 。如果您不想要这样的行为,请使用隐式命名策略。

我对命名策略进行了一些研究。你可以参考

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

如何使用 Hibernate5NamingStrategy

命名策略可以使用 StrategyOptions 。

例如,前缀(例如 f _ ):

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

其他示例: Hibernate 5隐式命名策略 除此之外, ImprovedNamingStrategy为休眠5 可用于模拟休眠的行为4 ImprovedNamingStrategy 。

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

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

followed the steps of SpringBoot 1.3.0 support hibernate 5?

I'm using following for naming Strategy

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

I've have a Entity class

@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; }

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) )

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) )

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 org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

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 AppUser). So you need to implement your own.

For an example a physical naming strategy

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

It uses NamingStrategyUtils.

Keep in mind, if you specify an explicit name

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

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

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 Hibernate5NamingStrategy

The naming strategy can be configured using StrategyOptions.

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

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

Other examples: Hibernate 5 Implicit Naming Strategy

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

更多推荐

Hibernate 5.1.x命名策略(向后兼容Hibernate 4.x)

本文发布于:2023-11-28 09:27:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:策略   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!