Java 如何使用camelCase将Hibernate实体字段映射到snake_case(下划线)数据库标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25681386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map Hibernate entity fields using camelCase to snake_case (underscore) database identifiers
提问by Paul Stanley
I have database fields in underscore. I have entity fields in camelcase. I can't change either of those.
我有下划线的数据库字段。我有驼峰式实体字段。这两个我都改不了。
Is there something, maybe a class level annotation I can use to default entity column name annotations to the camelcase equivalent?
有什么东西,也许我可以使用一个类级别的注释来将实体列名称注释默认为驼峰式等效项?
for example, I have an entity like this:
例如,我有一个这样的实体:
@Entity
public class AuthorisationEntity {
@Column(name = "non_recoverable")
private BigDecimal nonRecoverable;
@Column(name = "supplier_recoverable")
private BigDecimal supplierRecoverable;
@Column(name = "refund_amount")
private BigDecimal refundAmount;
}
I dream of this:
我有这样的梦想:
@Entity
@DatabaseIsUnderscoreAndThisAnnotationConvertsThemToCamelCaseByDefault
public class AuthorisationEntity {
private BigDecimal nonRecoverable;
private BigDecimal supplierRecoverable;
private BigDecimal refundAmount;
}
采纳答案by przemek hertel
You can use hibernate's naming strategy. Such naming strategy class describes how to generate database names for given java names.
您可以使用 hibernate 的命名策略。这样的命名策略类描述了如何为给定的 java 名称生成数据库名称。
See:
看:
very good oracle naming strategy- it converts camel to underscore convention, and much more
非常好的 oracle 命名策略- 它将骆驼转换为下划线约定,等等
回答by zawhtut
import org.hibernate.cfg.DefaultNamingStrategy;
import org.hibernate.cfg.ImprovedNamingStrategy;
public class NamingStratagyTest {
public static void main(String[] args) {
String colName = DefaultNamingStrategy.INSTANCE.columnName("UserName");
System.out.println(colName); // UserName
colName = ImprovedNamingStrategy.INSTANCE.columnName("UserName");
System.out.println(colName);// user_name
}
}
There you go, choose the naming strategy that suits your need.
好了,选择适合您需要的命名策略。
回答by Vlad Mihalcea
As I explained in this article, you can achieve this using a custom Hibernate naming strategy.
正如我在本文中所解释的,您可以使用自定义 Hibernate 命名策略来实现这一点。
All you need to do is to use the hibernate-typesopen-source project.
您需要做的就是使用hibernate-types开源项目。
Hibernate 5.2 or later
休眠 5.2 或更高版本
You need to add the following Maven dependency:
您需要添加以下 Maven 依赖项:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
And set the following Hibernate configuration property:
并设置以下 Hibernate 配置属性:
<property name="hibernate.physical_naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Hibernate 5.0 and 5.1
休眠 5.0 和 5.1
You need to add the following Maven dependency:
您需要添加以下 Maven 依赖项:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
And set the following Hibernate configuration property:
并设置以下 Hibernate 配置属性:
<property name="hibernate.physical_naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Hibernate 4.3
休眠 4.3
You need to add the following Maven dependency:
您需要添加以下 Maven 依赖项:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-43</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
And set the following Hibernate configuration property:
并设置以下 Hibernate 配置属性:
<property name="hibernate.ejb.naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Hibernate 4.2 and 4.1
休眠 4.2 和 4.1
You need to add the following Maven dependency:
您需要添加以下 Maven 依赖项:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-4</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
And set the following Hibernate configuration property:
并设置以下 Hibernate 配置属性:
<property name="hibernate.ejb.naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Testing time
测试时间
Assuming you have the following entities:
假设您有以下实体:
@Entity(name = "BookAuthor")
public class BookAuthor {
@Id
private Long id;
private String firstName;
private String lastName;
//Getters and setters omitted for brevity
}
@Entity(name = "PaperBackBook")
public class PaperBackBook {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE
)
private Long id;
@NaturalId
private String ISBN;
private String title;
private LocalDate publishedOn;
@ManyToOne(fetch = FetchType.LAZY)
private BookAuthor publishedBy;
//Getters and setters omitted for brevity
}
When using the CamelCaseToSnakeCaseNamingStrategycustom naming strategy, Hibernate is going to generate the following database schema using the hbm2ddltool:
使用CamelCaseToSnakeCaseNamingStrategy自定义命名策略时,Hibernate 将使用该hbm2ddl工具生成以下数据库模式:
CREATE SEQUENCE hibernate_sequence
START WITH 1 INCREMENT BY 1
CREATE TABLE book_author (
id BIGINT NOT NULL,
first_name VARCHAR(255),
last_name VARCHAR(255),
PRIMARY KEY (id)
)
CREATE TABLE paper_back_book (
id BIGINT NOT NULL,
isbn VARCHAR(255),
published_on DATE,
title VARCHAR(255),
published_by_id BIGINT,
PRIMARY KEY (id)
)
Cool, right?
酷,对吧?
回答by Leroy
I was experiencing the same problem in a Spring boot application, I tried adding the above Hibernate config to the spring properties file with no success, added it as a java bean, again with no success.
我在 Spring 启动应用程序中遇到了同样的问题,我尝试将上述 Hibernate 配置添加到 spring 属性文件中,但没有成功,将其添加为 java bean,再次没有成功。
I am using Hibernate Propertiesobject, adding the hibernate configuration within this solved my issue:
我正在使用 HibernateProperties对象,在其中添加休眠配置解决了我的问题:
protected Properties buildHibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
hibernateProperties.setProperty("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
return hibernateProperties;
}

