views:

51

answers:

1

I have a project I'm building with maven and I need to generate a schema using the hbm2ddl tool from hibernate3-maven-plugin.

I need to create database with a table called Order like the SQL keyword and I don't know how to make maven to quote this table when it's generating the script. I've done a search and I found there is a property in hibernate to tell the hbm2ddl tool to this, but I can't tell the plugin to use it:

<property name="hbm2ddl.keywords">auto-quote</property>

If I don't quote the table, hbm2ddl generates a script:

create table Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not null, supplier varchar(36) not null, product varchar(36) not null, forecast float, dateRaised date not null, dateDispatched date, dateReceived date, quantityOrdered double precision not null, quantitySent double precision, primary key (orderId)) ENGINE=InnoDB;

that doesn't compile (due an obvious syntax error):

02:51:41,264 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not null, supplier varchar(36) not null, product varchar(36) not null, forecast float, dateRaised date not null, dateDispatched date, dateReceived date, quantityOrdered double precision not null, quantitySent double precision, primary key (orderId)) ENGINE=InnoDB
02:51:41,264 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not' at line 1

This is the part of the pom.xml file:

<configuration>
 <components>
  <component>
   <name>hbm2java</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>src/main/java</outputDirectory>
  </component>
  <component>
   <name>hbm2ddl</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>src/main/resources</outputDirectory>
  </component>
  <component>
   <name>hbm2doc</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>docs/html/hibernate</outputDirectory>
  </component>
 </components>
 <componentProperties>
  <create>true</create>
  <drop>true</drop>
  <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
  <propertyfile>src/main/resources/database.properties</propertyfile>
  <jdk5>true</jdk5>
  <outputfilename>amasbe_db.sql</outputfilename>
 </componentProperties>
</configuration>

Any tips or help is really appreciated.

Thank you!

+1  A: 

AFAIK, the hbm2ddl.keywords is a NHibernate feature and is not supported by Hibernate.

With Hibernate, you'll have to quote the name yourself:

@Entity
@Table(name="`Order`")
public class Order {
    ...
}

The relevant section of the documentation is:

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>

References

Pascal Thivent
Well it's a pity it doesn't exist such a feature in hibernate, there is no sense, but thank you for the work around! :)
Bene
@Bene Yeah, I agree. You might want to track (and vote for) [HHH-2578](http://opensource.atlassian.com/projects/hibernate/browse/HHH-2578).
Pascal Thivent