簡體   English   中英

如何 map Hibernate 實體字段使用camelCase到snake_case(下划線)數據庫標識符

[英]How to map Hibernate entity fields using camelCase to snake_case (underscore) database identifiers

我有下划線的數據庫字段。 我在駝峰式中有實體字段。 我無法改變其中任何一個。

有什么東西,也許是 class 級別的注釋,我可以使用默認的實體列名注釋到駱駝大小寫等效項?

例如,我有一個這樣的實體:

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

}

我夢想這個:

@Entity
@DatabaseIsUnderscoreAndThisAnnotationConvertsThemToCamelCaseByDefault
public class AuthorisationEntity {

    private BigDecimal nonRecoverable;

    private BigDecimal supplierRecoverable;

    private BigDecimal refundAmount;

}

您可以使用自定義 Hibernate 命名策略來實現這一點。

您需要做的就是使用hibernate-types開源項目。

休眠 5.2 或更高版本

您需要添加以下 Maven 依賴項:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

並設置以下 Hibernate 配置屬性:

<property name="hibernate.physical_naming_strategy"
          value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>

休眠 5.0 和 5.1

您需要添加以下 Maven 依賴項:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-5</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

並設置以下 Hibernate 配置屬性:

<property name="hibernate.physical_naming_strategy"
          value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>

休眠 4.3

您需要添加以下 Maven 依賴項:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-43</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

並設置以下 Hibernate 配置屬性:

<property name="hibernate.ejb.naming_strategy"
          value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>

休眠 4.2 和 4.1

您需要添加以下 Maven 依賴項:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-4</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

並設置以下 Hibernate 配置屬性:

<property name="hibernate.ejb.naming_strategy"
          value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy"
/>

測試時間

假設您有以下實體:

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

當使用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)
)

酷,對吧?

您可以使用 hibernate 的命名策略。 這樣的命名策略類描述了如何為給定的 java 名稱生成數據庫名稱。

看:

命名策略示例

第二個例子

非常好的 oracle 命名策略- 它將駱駝轉換為下划線約定,等等

我在 Spring 啟動應用程序中遇到了同樣的問題,我嘗試將上述 Hibernate 配置添加到 spring 屬性文件中,但沒有成功,將其添加為 java bean,再次沒有成功。

我正在使用 Hibernate Properties對象,在其中添加休眠配置解決了我的問題:

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

好了,選擇適合您需要的命名策略。

從 Hibernate 5.5.4.Final ,引入了CamelCaseToUnderscoresNamingStrategy ,新策略相當於 Spring SpringPhysicalNamingStrategy

CamelCaseToUnderscoresNamingStrategy將所有點替換為下划線,所有駝峰大小寫替換為下划線,並生成所有小寫的表名。

例如, LineItem實體將映射到line_item表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM