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;  } 
                Implicit Naming StrategyHibernate uses a logical name to map an entity or attribute name to a table or column name. This name can be customized in two ways: it can be derived automatically by using an ImplicitNamingStrategy or it can be defined explicitly by using annotations.
PhysicalNamingStrategy. The idea of a PhysicalNamingStrategy is to define custom naming rules without having to hard-code them into the mapping via explicit names. Following is an implementation of PhysicalNamingStrategy to define custom table name and column name.
naming. strategy ; Hibernate 5 defines a Physical and Implicit naming strategies. Spring Boot configures SpringPhysicalNamingStrategy by default. This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel cases are replaced by underscores as well.
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>  And set the following Hibernate configuration property:
<property name="hibernate.physical_naming_strategy"           value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy" />  You need to add the following Maven dependency:
<dependency>     <groupId>com.vladmihalcea</groupId>     <artifactId>hibernate-types-5</artifactId>     <version>${hibernate-types.version}</version> </dependency>  And set the following Hibernate configuration property:
<property name="hibernate.physical_naming_strategy"           value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy" />  You need to add the following Maven dependency:
<dependency>     <groupId>com.vladmihalcea</groupId>     <artifactId>hibernate-types-43</artifactId>     <version>${hibernate-types.version}</version> </dependency>  And set the following Hibernate configuration property:
<property name="hibernate.ejb.naming_strategy"           value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy" />  You need to add the following Maven dependency:
<dependency>     <groupId>com.vladmihalcea</groupId>     <artifactId>hibernate-types-4</artifactId>     <version>${hibernate-types.version}</version> </dependency>  And set the following Hibernate configuration property:
<property name="hibernate.ejb.naming_strategy"           value="com.vladmihalcea.hibernate.type.util.CamelCaseToSnakeCaseNamingStrategy" />  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 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) )  Cool, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With