8

I am building data base system for electronic components. Unfortunatly other programs, that will use some of my tables need to have white spaces in column names. Ive tried in my hbm.xml file something like this with property:

...

property name="partGroup" column="part group" type="string"

...

of course hibernate wont create table with that column name.

Is there a way to do it using hibernate?

Thanks :]

7
  • Why do you say "of course"? Did you try it, it might work. Commented Aug 24, 2010 at 14:09
  • 1
    It's my subjective and completely personal opinion that hibernate is far more trouble than it's worth, and the wrong tool for 98% of jobs. Commented Aug 24, 2010 at 14:14
  • @skaffman Ive tried, it wont work, @Fasco, hibernate is not so bad, I found it useful in many of my projects, but white spaces was always pain in the ass... donno if it was solved, thats why I am asking :] Commented Aug 24, 2010 at 15:52
  • 3
    @Fosco Why do you even look at Hibernate questions then? Be at least consistent, don't look at them, and save us from dumb comments showing that you just don't get ORM. Thanks. Commented Aug 24, 2010 at 16:34
  • 2
    @Fosco Yes, you got it, this is all about me. Your comment is totally relevant and appropriate, it is actually the smartest comment in the world and it shows a deep knowledge of Hibernate. So because you make me speechless, I'm done now. Commented Aug 24, 2010 at 21:07

2 Answers 2

12

There is a way, enclose the table names or column names with backticks. From the documentation:

5.4. SQL quoted identifiers

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.

<class name="LineItem" table="`Line Item`">
    <id name="id" column="`Item Id`"/><generator class="assigned"/></id>
    <property name="itemNumber" column="`Item #`"/>
    ...
</class>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Pascal, stupid me :] thought quotes are only usefull when "case sensitive" names are important, didnt know that it also handles white spaces :] That will make my life in this project easier :)
0

I had solved this problem as follows:

  1. I had create following class:

    import org.hibernate.cfg.DefaultNamingStrategy;
    
    public class MysqlAdvancedNamingStrategy extends DefaultNamingStrategy{
    
    
    @Override
    public String columnName(String columnName) {
        return "`"+super.columnName(columnName)+"`";
    }
    
    @Override
    public String tableName(String tableName) {
        return "`"+super.tableName(tableName)+"`";
    }
    
    }
  2. I had setting as namingStrategy the "MysqlAdvancedNamingStrategy" at previous point.

The advantages of this solution is that you don't need to modify all annotation in you code.

It is also a clean solution because for some reason you may want have an application that access to same database "logic" in two different "database" (so with two style of escaping), with this solution you can intercept what database are you using and you can change the escaping strategy at "execution time", however the annotation are evalutated at "compilation time".

It is also a clean solution if you use Spring framework, with this you can change the escaping strategy without touching java code by setting a bean in session factory with id="namingStrategy" and class="util.MysqlAdvancedNamingStrategy".

Comments

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.