6

how do I force hibernate to generate db schema such that it converts CamelCase into Underscores (using HBM)? Eg. I have:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cz.csas.pdb.be.model.product.passive">

    <class name="foo.BarBaz">

        <id name="barBazId">
            <generator class="sequence"/>
        </id>

        <property name="extractContactType"/>
        <!-- ... -->
    </class>
</hibernate-mapping>

And I want hibernate to create table like this (oracle):

CREATE TABLE "BAR_BAZ"
  (
    "BAR_BAZ_ID"               NUMBER(19,0) NOT NULL ENABLE,
    "EXTRACT_CONTACT_TYPE"     VARCHAR2(512 CHAR),
    -- PK etc...
  )

I know I can use table/column name in the hbm.xml file, but I want to set it globally (both to save time and prevent errors).

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Ondrej Skalicka
  • 3,046
  • 9
  • 32
  • 53

2 Answers2

7

ImprovedNamingStrategy should do exactly what you want. See 3.6. Implementing a NamingStrategy.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • It looks like it should work ok, but I get an exception Caused by: org.hibernate.MappingException: Same logical column name referenced by different physical ones: account_charge.PASSIVEACCOUNT => 'passive_account' and 'passiveaccount'; for a simple many-to-one (accountCharge HBM has ""). – Ondrej Skalicka Mar 09 '11 at 10:24
6

In JPA 2 (Hibernate 4), it is even more easier, just add:

<property name="hibernate.ejb.naming_strategy"
         value="org.hibernate.cfg.ImprovedNamingStrategy" />

to your persistence.xml.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Sounds good, but I get a SAX-Parse-Exception which says that the attribute 'value' must be defined. Probably I have some old DTD or Schema for hibernate.cfg.xml. Using hibernate 4.3.5-Final. – user152468 Jul 06 '14 at 18:56
  • 1
    It's working flawlessly, allowed me to use camelCase on entities and snake_case on the database (postgresql), I'm using 4.3.8-Final – edur Mar 01 '15 at 02:44