Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible create column name with white spaces using hibernate?

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 :]

like image 722
Marek Avatar asked Aug 24 '10 14:08

Marek


People also ask

Can column name have space?

Column names can contain any valid characters (for example, spaces).

How to write a column name with space in SQL?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

How to get column name of table in Hibernate?

hibernate. type. Type[] columnTypes = getSessionFactory(). getClassMetadata(Employee.

Can table names have spaces in SQL?

Table names can contain any valid characters (for example, spaces).


1 Answers

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>
like image 77
Pascal Thivent Avatar answered Nov 15 '22 15:11

Pascal Thivent