Question

J'ai un projet que je construis avec Maven et j'ai besoin de générer un schéma à l'aide de l'outil HBM2DDL de HiberNate3-Maven-Plugin.

J'ai besoin de créer une base de données avec une table appelée Ordre Comme le mot clé SQL et je ne sais pas comment faire Maven pour citer ce tableau lorsqu'il génère le script. J'ai fait une recherche et j'ai trouvé qu'il y avait une propriété en hibernate pour en dire à l'outil HBM2DDL, mais je ne peux pas dire au plugin de l'utiliser:

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

Si je ne cite pas la table, HBM2DDL génère un 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;

Cela ne compile pas (en raison d'une erreur de syntaxe évidente):

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

Ceci est la partie du fichier pom.xml:

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

Tous les conseils ou l'aide sont vraiment appréciés.

Merci!

Était-ce utile?

La solution

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

Autres conseils

Another solution for the problem can be found here: https://forum.hibernate.org/viewtopic.php?p=2409922

Basically, all it needs is to derive a class that is in response for providing table name/column name.

Hope it help.

Cheers, Truong

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top