Pau*_*ley 27 java annotations hibernate
我在下划线中有数据库字段.我在camelcase中有实体字段.我无法改变其中任何一个.
有什么东西,也许是类级注释,我可以用来默认实体列名注释到camelcase等价物?
例如,我有一个像这样的实体:
@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;
}
Run Code Online (Sandbox Code Playgroud)
我梦见这个:
@Entity
@DatabaseIsUnderscoreAndThisAnnotationConvertsThemToCamelCaseByDefault
public class AuthorisationEntity {
private BigDecimal nonRecoverable;
private BigDecimal supplierRecoverable;
private BigDecimal refundAmount;
}
Run Code Online (Sandbox Code Playgroud)
prz*_*tel 13
您可以使用hibernate的命名策略.这种命名策略类描述了如何为给定的Java名称生成数据库名称.
看到:
非常好的oracle命名策略 - 它将camel转换为下划线约定等等
As I explained in this article, you can achieve this using a custom Hibernate naming strategy.
All you need to do is to use the hibernate-types
open-source project.
You need to add the following Maven dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
And set the following Hibernate configuration property:
<property name="hibernate.physical_naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Run Code Online (Sandbox Code Playgroud)
You need to add the following Maven dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
And set the following Hibernate configuration property:
<property name="hibernate.physical_naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Run Code Online (Sandbox Code Playgroud)
You need to add the following Maven dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-43</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
And set the following Hibernate configuration property:
<property name="hibernate.ejb.naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Run Code Online (Sandbox Code Playgroud)
You need to add the following Maven dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-4</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
And set the following Hibernate configuration property:
<property name="hibernate.ejb.naming_strategy"
value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
When using the CamelCaseToSnakeCaseNamingStrategy
custom naming strategy, Hibernate is going to generate the following database schema using the hbm2ddl
tool:
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)
)
Run Code Online (Sandbox Code Playgroud)
Cool, right?
我在 Spring boot 应用程序中遇到了同样的问题,我尝试将上述 Hibernate 配置添加到 spring 属性文件中,但没有成功,将其添加为 java bean,再次没有成功。
我正在使用 HibernateProperties
对象,在其中添加 hibernate 配置解决了我的问题:
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32270 次 |
最近记录: |