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).

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 "<many-to-one name="passiveAccount"/>").
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.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.