Search code examples
javahibernateannotations

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


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;

}

Solution

  • You can use hibernate's naming strategy. Such naming strategy class describes how to generate database names for given java names.

    See:

    naming strategy example

    second example

    very good oracle naming strategy - it converts camel to underscore convention, and much more